Java Stream
Java 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.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Types of Streams
Depending upon the data a stream holds, it can be classified into:
Byte Stream
Character Stream
Byte Stream
Byte stream is used to read and write a single byte (8 bits) of data.
All byte stream classes are derived from base abstract classes
called InputStream and 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.
OutputStream
Java application uses an output stream to write data to a destination; it
may be a file, an array, peripheral device or socket.
Java InputStream Class
The InputStream class of the java.io package is an abstract superclass that
represents an input stream of bytes.
Since InputStream is an abstract class, it is not useful by itself. However, its
subclasses can be used to read data.
Subclasses of InputStream
FileInputStream
ByteArrayInputStream
ObjectInputStream
Java OutputStream Class
Subclasses of OutputStream
FileOutputStream
ByteArrayOutputStream
ObjectOutputStream
Java Writer
Subclasses of Writer
BufferedWriter
OutputStreamWriter
FileWriter
StringWriter
Java Reader Class
The Reader class of the java.io package is an abstract superclass that represents
a stream of characters.
Since Reader is an abstract class, it is not useful by itself. However, its subclasses
can be used to read data.
Subclasses of Reader
BufferedReader
InputStreamReader
FileReader
StringReader
The BufferedWriter class of the java.io package can be used with other
writers to write data (in characters) more efficiently.
Java BufferedReader
The BufferedReader class of the java.io package can be used with other readers
to read data (in characters) more efficiently.
It extends the abstract class Reader .
Working of BufferedReader
br.close();
r.close();
}
}