Java I/O Streams - Complete Structured Notes with Examples
1. What is a Stream?
A stream is a flow of data between a source and a destination.
Data can be read (input) or written (output).
Example: keyboard -> input stream, screen -> output stream.
2. Java I/O Package
The [Link] package provides classes for handling I/O operations.
It contains two main stream types:
- Byte Streams (binary data)
- Character Streams (text data).
3. Character vs Byte Streams
Byte Stream: 8-bit binary data -> InputStream, OutputStream.
Character Stream: 16-bit Unicode -> Reader, Writer.
Example Code:
// Byte Stream Example
FileInputStream fin = new FileInputStream('[Link]');
FileOutputStream fout = new FileOutputStream('[Link]');
int byteData;
while((byteData = [Link]()) != -1) [Link](byteData);
[Link](); [Link]();
// Character Stream Example
FileReader fr = new FileReader('[Link]');
FileWriter fw = new FileWriter('[Link]');
int ch; while((ch = [Link]()) != -1) [Link](ch);
[Link](); [Link]();
4. IOException Class
Java I/O Streams - Complete Structured Notes with Examples
IOException occurs when an I/O operation fails (file missing, permission error, etc.).
It is a checked exception and must be handled.
Example:
try {
FileReader fr = new FileReader('[Link]');
int c; while((c = [Link]()) != -1) [Link]((char)c);
[Link]();
} catch(IOException e) {
[Link]('Error: ' + [Link]());
}
5. Byte Streams in Java
Used for 8-bit data like images.
Common classes: FileInputStream, FileOutputStream.
Example:
try (FileInputStream in = new FileInputStream('[Link]');
FileOutputStream out = new FileOutputStream('photo_copy.jpg')) {
int b;
while((b = [Link]()) != -1) [Link](b);
[Link]('File copied successfully!');
} catch(IOException e) { [Link](); }
6. Character Streams in Java
Used for 16-bit Unicode text.
Common classes: FileReader, FileWriter.
Example:
FileReader reader = new FileReader('[Link]');
FileWriter writer = new FileWriter('[Link]');
Java I/O Streams - Complete Structured Notes with Examples
int c; while((c = [Link]()) != -1) [Link](c);
[Link](); [Link]();
7. Line-Oriented I/O
To read line by line, use BufferedReader and readLine().
Example:
BufferedReader br = new BufferedReader(new FileReader('[Link]'));
String line;
while((line = [Link]()) != null) [Link](line);
[Link]();
8. Buffered I/O
Buffered I/O increases speed by reducing direct disk access.
BufferedReader and BufferedWriter are used for text data.
BufferedInputStream and BufferedOutputStream for binary data.
Example:
BufferedReader in = new BufferedReader(new FileReader('[Link]'));
BufferedWriter out = new BufferedWriter(new FileWriter('[Link]'));
String line;
while((line = [Link]()) != null) {
[Link](line);
[Link]();
}
[Link](); [Link]();
9. Standard I/O Streams
Three predefined streams in Java:
- [Link] (input stream from keyboard)
Java I/O Streams - Complete Structured Notes with Examples
- [Link] (output to console)
- [Link] (error messages)
Example:
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]('Enter name: ');
String name = [Link]();
[Link]('Hello, ' + name + '!');
10. Summary Table
Stream Type - Base Classes - Use
Byte Stream - InputStream/OutputStream - Binary data
Character Stream - Reader/Writer - Text data
Buffered Stream - BufferedReader/Writer - Fast I/O
Exception - IOException - Checked exception
Standard Streams - [Link]/out/err - Console I/O
References
Oracle Docs: [Link]
Book: Java Software Solutions by John Lewis & William Loftus