0% found this document useful (0 votes)
57 views

L21 - Java IO Console

This document discusses Java I/O and input/output streams in Java. It covers predefined streams like System.in, System.out, and System.err. It describes how to read input from the console using a BufferedReader and how to write output to the console using PrintStream. It also introduces the Scanner class for formatted input and the Console class for simplified console interactions.

Uploaded by

Win Bet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

L21 - Java IO Console

This document discusses Java I/O and input/output streams in Java. It covers predefined streams like System.in, System.out, and System.err. It describes how to read input from the console using a BufferedReader and how to write output to the console using PrintStream. It also introduces the Scanner class for formatted input and the Console class for simplified console interactions.

Uploaded by

Win Bet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java I/O: Console

CS F213: Object Oriented Programming


Instructor: Dr. Rahul Thakur
Predefined Streams
• All Java programs automatically import the java.lang package
• This package defines a class called System
• System also contains three predefined stream variables
• in, out, and err
• All these three are public, static, and final
• System.in refers to standard input (object of InputStream)
• By default, it is the keyboard
• System.out refers to the standard output stream (object of
PrintStream)
• By default, it is the console
• System.err refers to the standard error stream (object of
PrintStream)
• By default, it is the console

10-Nov-18 2
Reading Character and String
• To read a character from a BufferedReader
int read( ) throws IOException
• When read( ) is called, it reads a character from the
input stream and returns it as an integer value
• It returns -1 when the end of the stream is encountered
• To read a string from the keyboard
String readLine( ) throws IOException

10-Nov-18 3
Reading Character and String
BRRead.java
BRReadLines.java

10-Nov-18 4
Writing Console Output
• Console output is most easily accomplished with print( ) and
println( )
• System.out is an object of class PrintStream
• And PrintStream is derived from OutputStream
• And OutputStream implements the low-level method write( )
• Example:
class WriteDemo {
public static void main(String args[]) {
int b;
b = 'A';
System.out.write(b);
System.out.write('\n');
}
}

10-Nov-18 5
The Scanner Class
• Scanner class allows you to read formatted input and converts
it into its binary form
• Scanner can be used to read input from the console, a file, and
a string
• Creating a Scanner that reads the file Test.txt:
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin);
• Creating a Scanner that reads from standard input(keyboard)
Scanner conin = new Scanner(System.in);
• Creating a Scanner that reads from a string
String instr = "10 99.88 scanning is easy.";
Scanner conin = new Scanner(instr);
10-Nov-18 6
The Scanner Class
• In general, a Scanner reads tokens from the
underlying source
• To use Scanner, follow this general procedure:
• Determine if a specific type of input is available by
calling one of Scanner’s hasNextX methods
• Here X is the type of data desired (Boolean, Byte, Int, Double,
Float, Short, Long, ….. Even an entire line)
• If input is available, read it by calling one of Scanner’s
nextX methods
• Repeat the process until input is exhausted
• Close the Scanner by calling close( )

10-Nov-18 7
Scanner Example
AvgNums.java
scannerDemo.java

10-Nov-18 8
The Console Class
• The Console Class allows you to read from and
write to the console
• Most of the functionality of the Console class is
available through System.in and System.out
• Primarily a convenience class
• Simplifying some type of console interactions such as
reading strings from the console
• Console class doesn’t provide a constructor

10-Nov-18 9
The Console Class
• Obtain an object of Console class by calling the
console() in System class
static Console console()
• If console is available, then a reference to its object
will be returned
• Otherwise null is returned
• Methods defined in Console class
• readLine()
• printf()
• readPassword()

10-Nov-18 10
The Console Example
consoleDemo.java

10-Nov-18 11

You might also like