Home  >  Core Java

Example of Console in Java

By Arvind Rai, January 25, 2013
Console has been introduced in jdk1.6. It works like BufferedReader. Console does not throw compile time exception. It has methods like readLine,format,readPassword etc. The object of Console is obtained from System.console. It works only when we are working in console based environment. If we work in any IDE like eclipse etc, then Console will not work and System.console will return null.
Console class has a method readPassword, that can be used to read password. It will nor display input while taking input on console.
import java.io.Console;

public class ConsoleTest {
	public static void main(String... args){
		Console con=System.console();
		System.out.println("Enter user Name.");
		String user=con.readLine();
		System.out.println("Enter password.");
		char[] pwd=con.readPassword();


©2022 concretepage.com | Privacy Policy | Contact Us