Java IO Tutorial - Java Console.readPassword()








Syntax

Console.readPassword() has the following syntax.

public char[] readPassword()

Example

In the following code shows how to use Console.readPassword() method.

import java.io.Console;
/*from   www  .  j  av a  2s. c om*/
public class Main {
  public static void main(String[] args) throws Exception {

    Console cnsl = System.console();

    if (cnsl != null) {

      String alpha = cnsl.readLine("Name: ");

      System.out.println("Name is: " + alpha);

      char[] pwd = cnsl.readPassword("Password: ");

      System.out.println("Password is: " + pwd);
    }
  }
}

The code above generates the following result.