Java Console readPassword() Example
Console class readPassword() method example. This example shows you how to use readPassword() method.
Syntax is : public char[] readPassword()
Method reads a password or passphrase from the console with echoing disabled and returns a character array containing the password or passphrase read from the console, not including any line-termination characters, or null if an end of stream has been reached.
Here is the code.
/**
* @(#) ReadPasswordConsole.java
* A class representing use of method readPassword() of Console class in
java.io Package.
* @Version 11-June-2008
* @author Rose India Team
*/
import java.io.*;
public class ReadPasswordConsole {
public static void main(String[] args) {
//Create a console object.
Console objConsole = System.console();
if (objConsole == null) {
System.err.println("Console Object is not available.");
System.exit(1);
} else {
String userName = objConsole.readLine("Username : ");
System.out.println("You have entered : " + userName);
// readPassword method call.
char[] prePwd = objConsole.readPassword("Password : ");
System.out.println("Password is : " + prePwd);
}
}
} |
Output of the program.
Username : mahendra
You have entered : mahendra
Password :
Password is : mahendra |
|