In this Java tutorial, learn different ways to read from and write to the system console. A console is generally connected with Java processes which are started using the command-line tool.
If the Java process has started automatically, (for example a background task), the console may not be available for input and output purposes.
1. Java read input from console
By default, to read from system console, we can use the Console
class. This class provides methods to access the character-based console, if any, associated with the current Java process. To get access to Console
, call the method System.console().
Console
gives three ways to read the input:
String readLine()
– reads a single line of text from the console.char[] readPassword()
– reads a password or encrypted text from the console with echoing disabledReader reader()
– retrieves theReader
object associated with this console. This reader is supposed to be used by sophisticated applications.For example,
Scanner
object which utilizes the rich parsing/scanning functionality on top of the underlyingReader
.
Java program to read console input with readLine()
Console console = System.console(); if(console == null) { System.out.println("Console is not available to current JVM process"); return; } String userName = console.readLine("Enter the username: "); System.out.println("Entered username: " + userName);
Program output
Enter the username: lokesh Entered username: lokesh
Java program to read console input with readPassword()
Console console = System.console(); if(console == null) { System.out.println("Console is not available to current JVM process"); return; } char[] password = console.readPassword("Enter the password: "); System.out.println("Entered password: " + new String(password));
Program output
Enter the password: //input will not visible in the console Entered password: passphrase
Java program to read console input with reader()
Console console = System.console(); if(console == null) { System.out.println("Console is not available to current JVM process"); return; } Reader consoleReader = console.reader(); Scanner scanner = new Scanner(consoleReader); System.out.println("Enter age:"); int age = scanner.nextInt(); System.out.println("Entered age: " + age); scanner.close();
Program output
Enter age: 12 Entered age: 12
2. Java print output to console
The easiest way to write the output data to console is System.out.println()
statements. Still, we can use printf()
methods to write formatted text to console.
Java program to write to console with System.out.println
System.out.println("Hello, world!");
Program output
Hello, world!
Java program to write to console with printf()
The printf(String format, Object... args)
method takes an output string and multiple parameters which are substituted in the given string to produce the formatted output content. This formatted output is written in the console.
String name = "Lokesh"; int age = 38; console.printf("My name is %s and my age is %d", name, age);
Program output
My name is Lokesh and my age is 38
The above listed methods for reading the input and writing output to the console provide alot of flexibility to read inputs in different formats and in different ways.
I will suggest you to play with given code to understand it better.
Happy Learning !!