0% found this document useful (0 votes)
128 views6 pages

UNIT-3 - Reading and Writing Console

Uploaded by

Jayanthi M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views6 pages

UNIT-3 - Reading and Writing Console

Uploaded by

Jayanthi M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Console Class

Java Console Class


• The Java Console class is be used to get input from console. It
provides methods to read texts and passwords.
• If you read password using Console class, it will not be displayed to
the user.
• The java.io.Console class is attached with system console internally.

Let's see a simple example to read text from console.


String text=System.console().readLine();
System.out.println("Text is: "+text);

Java Console class declaration


• Let's see the declaration for Java.io.Console class:
• public final class Console extends Object implements Flushable
Java Console class methods
Method Description
It is used to retrieve the reader object associated with
Reader reader()
the console
String readLine() It is used to read a single line of text from the console.
String readLine(String It provides a formatted prompt then reads the single
fmt, Object... args) line of text from the console.
It is used to read password that is not being displayed
char[] readPassword()
on the console.
char[]
It provides a formatted prompt then reads the
readPassword(String fmt,
password that is not being displayed on the console.
Object... args)
Console format(String It is used to write a formatted string to the console
fmt, Object... args) output stream.
Console printf(String
It is used to write a string to the console output stream.
format, Object... args)
It is used to retrieve the PrintWriter object associated
PrintWriter writer()
with the console.
void flush() It is used to flushes the console.
Java Console Example

import java.io.Console;
class ReadStringTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter your name: ");
String n=c.readLine();
System.out.println("Welcome "+n);
}
}

Output
Enter your name: Nakul Jain
Welcome Nakul Jain
Java Console Example to read password

import java.io.Console;
class ReadPasswordTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter password: ");
char[] ch=c.readPassword();
String pass=String.valueOf(ch);//
converting char array into string
System.out.println("Password is: "+pass);
}
}
Output
Enter password:
Password is: 123

You might also like