ADS

How To Read Password In Console In Java?

The following illustrates reading password in console in Java which means that password isn't visible when the user is typing.
class ReadPassword
{
    public static void main(String args[])
    {
        System.out.println("Enter password");
       
        char[] c=System.console().readPassword();
       
        // Just print, make it simple
        System.out.println("The password is "+new String(c));
       
    }
}
System.console().readPassword(): The console() method generates a Console object which contains the readPassword() method. This method reads the password from the user into a char array and returns it.

Subscribe to receive free email updates:

ADS