What is Console class?
– JavaSE 6 adds the Console class. It is used to read from and write to the console, if one exists.
– It implements the Flushable interface.
– Console is primarily a convenience class because most of its functionality is available through System.in and System.out;
– Console supplies no Constructor.
– Console object is obtained by calling System.Console();
ConsoleDemo.java
import java.io.*;
class ConsoleDemo {
public static void main(String args[]) {
String str;
Console con;
con = System.console();
// If no console available, exit.
if (con == null)
return;
str = con.readLine("Enter a string: ");
con.printf("Here is your string: %s\n", str);
System.out.println("Please Enter Password: ");
char cpass[] = con.readPassword();
System.out.println("Your Password is : ");
for (int i = 0; i < cpass.length; i++)
System.out.print(cpass[i]);
}
}
Output :
C:\>javac ConsoleDemo.java
C:\>java ConsoleDemo
Enter a string: Good morning friends
Here is your string: Good morning friends
Please Enter Password:
Your Password is :
javaskool123
C:\>
Recent Comments