Java I/O Streams
Understanding I/O Streams in Java
Input and Output (I/O) operations are essential in Java applications. They allow programs to read data from external sources and write data to destinations such as files, network connections, or system output.
In earlier sections, you learned how to create, read, and write simple files. However, Java provides a more flexible mechanism for handling data using I/O Streams.
An I/O Stream is a flow of data between a program and a data source or destination.
- Input Stream → Reads data into a program
- Output Stream → Writes data from a program to a destination
File Class vs I/O Streams
The File class is used to manage file and directory information, such as:
- Checking whether a file exists
- Retrieving file names and sizes
- Creating or deleting files and folders
However, the File class does not read or write the contents of a file. It only provides information about the file system.
To actually read or write data, Java uses I/O stream classes.
For example:
FileWriter – writes text to a file
Scanner – reads text from a file
While these classes are convenient for simple text files, I/O streams offer greater flexibility
because they support both text and binary data.
Types of I/O Streams in Java
Java streams are broadly divided into two categories:
- Byte Streams
- Character Streams
Understanding the difference between them is important for choosing the correct class when working with files.
1.Byte Streams
Byte streams work with raw binary data. They are typically used when working with non-text files such as images, videos, audio files, or PDFs.
Byte streams process data one byte at a time, which makes them suitable for handling any type of file format.
Common Byte Stream Classes
- FileInputStream – reads binary data from a file
- FileOutputStream – writes binary data to a file
Example: Writing Binary Data to a File
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamExample {
public static void main(String[] args) {
try (FileOutputStream stream = new FileOutputStream("binaryData.bin")) {
byte[] numbers = {10, 20, 30, 40, 50};
stream.write(numbers);
System.out.println("Binary data written successfully.");
} catch (IOException error) {
System.out.println("Error writing binary data.");
error.printStackTrace();
}
}
}
Output
Binary data written successfully.
This program writes a sequence of raw bytes into a file named binaryData.bin.
2. Character Streams
Character streams are designed for working with text data. They automatically handle character encoding and are easier to use when dealing with strings and textual information.
Character streams process data character by character rather than byte by byte.
Common Character Stream Classes
- FileReader – reads text from a file
- FileWriter – writes text to a file
- BufferedReader – efficient reading of text files
- BufferedWriter – efficient writing of text files
Example: Reading Text Using a Character Stream
import java.io.FileReader;
import java.io.IOException;
public class CharacterStreamExample {
public static void main(String[] args) {
try (FileReader reader = new FileReader("message.txt")) {
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
} catch (IOException error) {
System.out.println("Error reading the file.");
error.printStackTrace();
}
}
}
Output
Welcome to Java file streams.
Learning I/O operations step by step.
This example reads characters from a text file and displays them on the console.