5.2 File IO Explained
File Input/Output (IO) in Java is essential for reading from and writing to files. Understanding how to handle file IO efficiently is crucial for developing robust and scalable applications. This section will cover key concepts related to File IO, providing detailed explanations and examples.
Key Concepts
1. FileInputStream and FileOutputStream
FileInputStream and FileOutputStream are used for reading and writing binary data to files. FileInputStream reads bytes from a file, while FileOutputStream writes bytes to a file. These classes are part of the java.io package.
Example
import java.io.*;
public class Main {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("output.txt")) {
int data;
while ((data = fis.read()) != -1) {
fos.write(data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. FileReader and FileWriter
FileReader and FileWriter are used for reading and writing character data to files. FileReader reads characters from a file, while FileWriter writes characters to a file. These classes are also part of the java.io package.
Example
import java.io.*;
public class Main {
public static void main(String[] args) {
try (FileReader fr = new FileReader("input.txt");
FileWriter fw = new FileWriter("output.txt")) {
int data;
while ((data = fr.read()) != -1) {
fw.write(data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. BufferedReader and BufferedWriter
BufferedReader and BufferedWriter are used to wrap FileReader and FileWriter, respectively, to provide buffering for improved performance. Buffering reduces the number of I/O operations, making file reading and writing more efficient.
Example
import java.io.*;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. DataInputStream and DataOutputStream
DataInputStream and DataOutputStream are used for reading and writing primitive data types to files. These classes provide methods to read and write data in a machine-independent format.
Example
import java.io.*;
public class Main {
public static void main(String[] args) {
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin"));
DataInputStream dis = new DataInputStream(new FileInputStream("data.bin"))) {
dos.writeInt(123);
dos.writeDouble(456.789);
int intValue = dis.readInt();
double doubleValue = dis.readDouble();
System.out.println(intValue + " " + doubleValue); // Output: 123-456.789
} catch (IOException e) {
e.printStackTrace();
}
}
}
5. File and Path
The File class in Java represents a file or directory path. It provides methods to create, delete, and manipulate files and directories. The Path interface in Java NIO represents a file or directory path and provides methods to perform file operations.
Example
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
File file = new File("example.txt");
System.out.println(file.exists()); // Check if file exists
Path path = Paths.get("example.txt");
System.out.println(path.toAbsolutePath()); // Get absolute path
}
}
Examples and Analogies
Think of FileInputStream and FileOutputStream as pipes that transport raw materials (binary data) from one place to another. FileReader and FileWriter are like conveyor belts that move finished products (character data) more carefully. BufferedReader and BufferedWriter are like storage bins on the conveyor belts, allowing products to be processed in batches for efficiency. DataInputStream and DataOutputStream are like specialized machines that handle different types of materials (primitive data types) in a standardized way. The File class and Path interface are like blueprints and tools used to build and manage the entire production line.
By mastering File IO in Java, you can develop applications that handle file operations efficiently and reliably.