If you want to read password using Console class, it will not be displayed to the user end.

The Console class is internally attached with system console. Console class is defined in java.io package.

Let’s we take example to read user name and password given by the user end.

import java.io.*;  
class ConsoleClassEx{  
public static void main(String args[]){  
Console c=System.console();
System.out.println("Enter your name: ");  
String n=c.readLine();  
System.out.println("Enter password: ");  
char[] ch=c.readPassword(); //It's not shown  user end
String pass=String.valueOf(ch);//converting char array into string  
System.out.println("Welcome "+n); 
System.out.println("Password is: "+pass);  
}  }   
Java Console Class

  Modified On Sep-07-2019 12:16:01 AM

Leave Comment