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:
- public String readLine(): Reads a line from console.
- public String readLine(String fmt,Object… args): It provides a formatted prompt then reads the single line of text from the console.
- public char readPassword(): Reads password from console. Password is not visible at console.
- 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?
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);
Have any question or suggestion for us?Please feel free to post in Q&A Forum
[…] previous post , we have see Console class in java. In this post, we will see usage of Scanner […]