Java I/O Streams
In Java, streams are the sequence of data that are read from the source and written to the destination.
An input stream is used to read data from the source. And, an output stream is used to write data to the destination.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
For example, in our first Hello World example, we have used System.out to print a string. Here, the System.out is a type of output stream.
Similarly, there are input streams to take input.
We will learn about input streams and output streams in detail in the later tutorials.
Before exploring various input and output streams lets look at 3 standard or default streams that Java has to provide which are also most common in use:
- System.in: This is the standard input stream that is used to read characters from the keyboard or any other standard input device.
- System.out: This is the standard output stream that is used to produce the result of a program on an output device like the computer screen.
- System.err: This is the standard error stream that is used to output all the error data that a program might throw, on a computer screen or any standard output device.
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:
InputStream is an abstract class of Byte Stream that describe stream input and it is used for reading and it could be a file, image, audio, video, webpage, etc. it doesn’t matter. Thus, InputStream read data from source one item at a time.
OutputStream:
OutputStream is an abstract class of Byte Stream that describes stream output and it is used for writing data to a file, image, audio, etc. Thus, OutputStream writes data to the destination one at a time.
Difference between InputStream and OutputStream
InputStream |
OutputStream |
1. It is an abstract class that
describes Stream Input. |
1. It is an abstract class that
describes Stream Output. |
2. InputStream Read data from the
source once at a time. |
2. OutputStream Write Data to the
destination once at a time. |
3. InputStream consist of method
which performs:
|
3. Output Stream consists of
methods which perform:
|
4. Types of InputStream are:
In these types the most important
and mostly used type is FileInputStream.
|
4. Types of OutputStream are:
In these types the most important
and mostly used type is FileOutput Stream. |
Character Stream
In Java, characters are stored using Unicode conventions (Refer this for details). Character stream automatically allows us to read/write data character by character. Though it has many classes, the FileReader and the FileWriter are the most popular ones. FileReader and FileWriter are character streams used to read from the source and write to the destination respectively.
Stream class Description BufferedReader It is used to handle buffered
input stream. FileReader This is an input stream that reads
from file. InputStreamReader This input stream is used to
translate byte to character. OutputStreamReader This output stream is used to
translate character to byte. Reader This is an abstract class that
define character stream input. PrintWriter This contains the most used
print() and println() method Writer This is an abstract class that
define character stream output. BufferedWriter This is used to handle buffered
output stream. FileWriter This is used to output stream that
writes to file.
0 Comments