Java I/O
Java I/O (Input/Output) is used to read data from input sources and write data to output destinations. Java provides I/O classes mainly through the java.io package.
I/O can be used with files, keyboard input, memory, network streams, and other data sources.
Basic I/O Concepts
Input → Java Program → Output
- Input: Data received by a program.
- Output: Data produced by a program.
- Stream: A flow of data between a source and a destination.
java.io Package
The java.io package contains classes and interfaces for performing input and output operations.
Some commonly used classes are:
| Class | Purpose |
|---|---|
File | Represents files and directories |
FileReader | Reads character data |
FileWriter | Writes character data |
BufferedReader | Reads text efficiently |
BufferedWriter | Writes text efficiently |
FileInputStream | Reads byte data |
FileOutputStream | Writes byte data |
InputStream | Base class for byte input |
OutputStream | Base class for byte output |
PrintWriter | Writes formatted text |
Types of Streams
Java I/O streams are mainly divided into two types:
1. Byte Stream
Byte streams handle data as 8-bit bytes. They are commonly used for binary files such as images, audio, and video.
Important classes:
FileInputStream FileOutputStream
Example
import java.io.FileInputStream; import java.io.IOException; class Main { public static void main(String[] args) { try { FileInputStream input = new FileInputStream("data.txt"); int data; while ((data = input.read()) != -1) { System.out.print((char) data); } input.close(); } catch (IOException e) { System.out.println("Error reading file"); } } }
2. Character Stream
Character streams handle character-based text data.
Important classes:
FileReader FileWriter
Example
import java.io.FileReader; import java.io.IOException; class Main { public static void main(String[] args) { try { FileReader reader = new FileReader("data.txt"); int data; while ((data = reader.read()) != -1) { System.out.print((char) data); } reader.close(); } catch (IOException e) { System.out.println("Error reading file"); } } }
Buffered I/O
Buffered streams use a memory buffer to make input and output operations more efficient.
BufferedReader Example
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; class Main { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader("data.txt")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { System.out.println("Error reading file"); } } }
Standard I/O
Java provides three standard I/O streams:
System.in
Used for standard input, usually the keyboard.
int data = System.in.read();
System.out
Used for standard output.
System.out.println("Hello Java");
System.err
Used for error messages.
System.err.println("Error occurred");
Byte Stream vs Character Stream
| Byte Stream | Character Stream |
|---|---|
| Handles bytes. | Handles characters. |
| Suitable for binary data. | Suitable for text data. |
Uses InputStream and OutputStream. | Uses Reader and Writer. |
Examples: FileInputStream, FileOutputStream. | Examples: FileReader, FileWriter. |
| Commonly used for images, audio, and video. | Commonly used for text files. |
Advantages of Java I/O
- Supports file input and output.
- Supports both text and binary data.
- Provides buffered I/O for better performance.
- Supports keyboard and console input/output.
- Provides classes for file and directory handling.
- Makes data storage and retrieval possible.
Key Points
- Java I/O is mainly provided through the
java.iopackage. - A stream represents a flow of data.
- Byte streams handle binary data.
- Character streams handle text data.
- Buffered streams improve I/O efficiency.
System.in,System.out, andSystem.errare standard I/O streams.- Many I/O operations can throw
IOException.
Conclusion
Java I/O provides a powerful set of classes for handling input, output, files, and streams. Understanding byte streams, character streams, and buffered streams is essential for working with files and external data in Java.