Java Console readPassword() Example
Console class readPassword() method example. This example shows you how to use readPassword() method.
Syntax is : public char[] readPassword(String fmt, Object... args)
This method provides a formatted prompt, then reads a password or passphrase from the console with echoing disabled.
Here is the code.
/**
* @(#) ReadPassword1Console.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 ReadPassword1Console {
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 PASS_PROMPT = "%1$4s %2$4s %3$8s%n";
// readPassword method call.
char[] pwdArray = objConsole.readPassword(PASS_PROMPT, "Enter",
"user", "Password :");
System.out.print("You have entered password : ");
System.out.println(pwdArray);
}
}
} |
Output of the program.
Enter user Password :
You have entered password : mahendra |
|