Console Class in java

In the previous post, we have seen CharArrayWriter class. In this post, we will see the usage of Console class.

In java, Console class is used to read the input provided from console by the user. It can be used to data like text and password. Typed password will not be visible on the console.

Console class Methods:

  1. public String readLine(): Reads a line from console.
  2. public String readLine(String fmt,Object… args): It provides a formatted prompt then reads the single line of text from the console.
  3. public char[] readPassword(): Reads password from console. Password is not visible at console.
  4. public char[] readPassword(String fmt,Object… args): It provides a formatted prompt then reads the password that is not being displayed on the console.

How to create instance of a console class?

Console c=System.console();

Read the text from Console:

		System.out.println("Enter book name : "); 
		String Book=System.console().readLine();  
		 
		System.out.println("You like "+Book);

Read password from console:

		Console c=System.console();  
		System.out.println("Enter password: ");  
		char[] chPwd=c.readPassword();  
		String pass=String.valueOf(chPwd);//converting char array into string  
		System.out.println("Password is: "+pass);
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

1 Response

  1. August 8, 2015

    […] previous post , we have see Console class in java. In this post, we will see usage of Scanner […]