Console class can read from and write to the console.
Console class's functionality is available through System.in and System.out.
Its use can simplify some types of console interactions, especially when reading strings from the console.
A Console object is obtained by calling System.console()
, which is shown here:
static Console console()
// Demonstrate Console. import java.io.Console; public class Main { public static void main(String args[]) { String str;// w w w. ja v a 2 s .c o m Console con; // Obtain a reference to the console. con = System.console(); // If no console available, exit. if (con == null) return; // Read a string and then display it. str = con.readLine("Enter a string: "); con.printf("Here is your string: %s\n", str); } }