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.
It is important to note that if the Java process has started automatically, (for example a background task), the console may not be available for input and output purposes. In such cases, System.console() will return null.
1. Reading 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
.
1.1. Reading 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
1.2. Reading 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
1.3. Read 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. Writing 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.
2.1. Writing with System.out.println
System.out.println("Hello, world!");
Program output
Hello, world!
2.2. Writing 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 !!