Input Output
Input Output
G A N E S H PA I
A S S T. P R O F E S S O R G D I I I
D E PA RT M E N T O F C S E
N M A M I T, N I T T E
Overview of topics
I/O Basics
Reading Console Input
Writing Console Output
Print Writer Class
One of the concrete subclass of abstract class Reader is InputStreamReader, which converts bytes to
characters.
InputStreamReader object linked to System.in is obtained using the following constructor:
InputStreamReader(InputStream inputStream)
Combining all, the following line of code creates a BufferedReader that is connected to the keyboard:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br is a character-based stream that is linked to the console through System.in.
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 10
Reading Characters
To read a character from a BufferedReader, use read( ).
int read( ) throws IOException
Each time read( ) is called,
it reads a character from the input stream
returns it as an integer value
returns –1 when the end of stream is encountered
// read characters
do {
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}
}
If an argument is not a simple type, the PrintWriter methods call the object’s toString( ) method and then
display the result.
To write to the console by using a PrintWriter, specify System.out for the output stream and automatic flushing.
pw.println("This is a string");
int i = -7;
pw.println(i);
double d = 4.5e-7;
pw.println(d);
}
}