7.1 Inputoutput Stream
7.1 Inputoutput Stream
Java uses the concept of a stream to make I/O operation fast. The java.io package
contains all the classes required for input and output operations.
Stream
In java 3 streams are created for us automatically. All these streams are attached
with the console.
Let's see the code to print output and an error message to the console.
1. System.out.println("simple message");
2. System.err.println("error message");
1. int i=System.in.read();//returns ASCII code of 1st character
2. System.out.println((char)i);//will print the character
OutputStream vs InputStream
OutputStream
InputStream
Java application uses an input stream to read data from a source; it may be a file,
an array, peripheral device or socket.
Let's understand the working of Java OutputStream and InputStream by the figure
given below.
OutputStream class
Method Description
OutputStream Hierarchy
InputStream class
Method Description
1) public abstract int read()throws reads the next byte of data from the input stream. It
IOException returns -1 at the end of the file.
2) public int available()throws returns an estimate of the number of bytes that can be
IOException read from the current input stream.
InputStream Hierarchy