Introducing Java I/O
Java I/O stands for Java Input/Output. In the previous chapters, we were actually using Java’s I/O system but without knowing much about it. Java’s I/O system is very elaborate and it is not possible to discuss each and every API Java provides. We will discuss the basic and important APIs here, and it is your responsibility to explore more as a Java developer.
Java I/O system contains many built-in classes. We can classify the Java I/O into two categories as shown below.
- Text based
- File based
Java performs I/O operations through streams. An I/O stream is an abstraction that produces or consumes information. A stream is directly linked to a physical device using Java I/O system. All streams behave in the same manner to whatever device it is connected to. The implementation of I/O streams is done in java.io package.
The Byte Stream Classes
Byte streams are defined by two main abstract classes InputStream and OutputStream. InputStream defines common classes that consume byte input from devices whereas OutputStream defines classes to write a byte to devices. There are so many classes derived from these two streams, but they all work in the same manner. If you learn one class then you can easily understand the remaining.
Below is the list of Byte stream classes.
|Byte Stream Class||Meaning|
|BufferedInputStream||Buffered input stream|
|BufferedOutputStream||Buffered output stream|
|ByteArrayInputStream||Input stream that reads from a byte array|
|ByteArrayOutputStream||Output stream that writes to a byte array|
|DataInputStream||An input stream that contains methods for reading the Java
standard data types
|DataOutputStream||An output stream that contains methods for writing the Java
standard data types
|FileInputStream||Input stream that reads from a file|
|FileOutputStream||Output stream that writes to a file|
|InputStream||Abstract class that describes stream input|
|ObjectInputStream||Input stream for objects|
|ObjectOutputStream||Output stream for objects|
|OutputStream||Abstract class that describes stream output|
|PrintStream||Output stream that contains print() and println()|
|PushbackInputStream||Input stream that allows bytes to be returned to the stream|
|SequenceInputStream||Input stream that is a combination of two or more input streams that
will be read sequentially, one after the other
The Character Stream
Character streams consume character data. They have two main abstract classes: Reader and Writer. We use Reader for input and Writer for output.
Below are the list of character stream classes.
|Character Stream Class||Meaning|
|BufferedReader||Buffered input character stream|
|BufferedWriter||Buffered output character stream|
|CharArrayReader||Input stream that reads from a character array|
|CharArrayWriter||Output stream that writes to a character array|
|FileReader||Input stream that reads from a file|
|FileWriter||Output stream that writes to a file|
|InputStreamReader||Input stream that translates bytes to characters|
|LineNumberReader||Input stream that counts lines|
|OutputStreamWriter||Output stream that translates characters to bytes|
|PrintWriter||Output stream that contains print() and println()|
|PushbackReader||Input stream that allows characters to be returned to the input stream|
|Reader||Abstract class that describes character stream input|
|StringReader||Input stream that reads from a string|
|StringWriter||Output stream that writes to a string|
|Writer||Abstract class that describes character stream output|
You can learn more information about Java I/O from Oracle Java help site.