Java I/O Package - Complete Notes
Java I/O (Input and Output) is used to process the input and produce the output.
Java uses the concept of streams to make I/O operations fast. The java.io package contains all the
classes required for input and output operations.
Two types of Streams:
1. Byte Streams - handle I/O of raw binary data.
2. Character Streams - handle I/O of character data, automatically handling character encoding and
decoding.
Byte Stream Classes
Byte streams use InputStream and OutputStream as base classes.
Common classes include:
- FileInputStream
- FileOutputStream
- BufferedInputStream
- BufferedOutputStream
- DataInputStream
- DataOutputStream
Image not found: /mnt/data/byte_stream_hierarchy.png
Character Stream Classes
Character streams use Reader and Writer as base classes.
Common classes include:
- FileReader
- FileWriter
- BufferedReader
- BufferedWriter
- PrintWriter
Image not found: /mnt/data/character_stream_hierarchy.png
File I/O with Constructors
FileInputStream fis = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("output.txt");
FileReader fr = new FileReader("input.txt");
FileWriter fw = new FileWriter("output.txt");
Important Classes and Interfaces in java.io
- InputStream / OutputStream
- Reader / Writer
- File
- Serializable
- BufferedReader / BufferedWriter
- PrintStream / PrintWriter
- ObjectInputStream / ObjectOutputStream