Thursday 14 November 2013

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.

No comments:

Post a Comment