Reading and Writing Files
Reading from and writing to files are fundamental operations in Java programming. Whether you're processing text files, handling binary data, or automating file-based workflows, understanding how to perform these operations efficiently is crucial.
In this section, weβll explore:
- Techniques for reading text files using
FileReader,BufferedReader, andScanner. - Techniques for writing text files using
FileWriter,BufferedWriter, andPrintWriter. - Reading and writing binary files using
FileInputStreamandFileOutputStream.
Reading Text Filesβ
Java provides multiple ways to read text files, depending on your requirements. Below are some common approaches:
1. Using FileReaderβ
The FileReader class reads characters from a file. Itβs suitable for small files but less efficient for large ones.
Example: Reading a File Using FileReaderβ
import java.io.FileReader;
import java.io.IOException;
public class FileReaderExample {
public static void main(String[] args) {
try (FileReader reader = new FileReader("input.txt")) {
int content;
while ((content = reader.read()) != -1) {
System.out.print((char) content);
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Contents of input.txt
Explanation:
- The
FileReaderreads one character at a time until the end of the file (-1).
2. Using BufferedReaderβ
The BufferedReader class improves performance by buffering data during reads. Itβs ideal for reading large files line by line.
Example: Reading a File Using BufferedReaderβ
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Line 1 of input.txt
Line 2 of input.txt
...
Explanation:
- The
BufferedReaderreads the file line by line using thereadLine()method.
3. Using Scannerβ
The Scanner class is useful for parsing structured data, such as CSV files or user input.
Example: Reading a File Using Scannerβ
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(new File("input.txt"))) {
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Line 1 of input.txt
Line 2 of input.txt
...
Explanation:
- The
Scannerreads the file line by line using thenextLine()method.
Writing Text Filesβ
Writing to files is just as important as reading them. Below are some common techniques for writing text files:
1. Using FileWriterβ
The FileWriter class writes characters to a file. Itβs simple but less efficient for large files.
Example: Writing to a File Using FileWriterβ
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("output.txt")) {
writer.write("Hello, World!");
System.out.println("Data written to file.");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Data written to file.
Explanation:
- The
FileWriterwrites text directly to the file.
2. Using BufferedWriterβ
The BufferedWriter class improves performance by buffering data before writing it to the file.
Example: Writing to a File Using BufferedWriterβ
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("Hello, World!");
writer.newLine();
writer.write("This is a buffered writer example.");
System.out.println("Data written to file.");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Data written to file.
Explanation:
- The
BufferedWriterwrites text efficiently and adds a newline usingnewLine().
3. Using PrintWriterβ
The PrintWriter class provides convenient methods like println() for writing formatted text.
Example: Writing to a File Using PrintWriterβ
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
public class PrintWriterExample {
public static void main(String[] args) {
try (PrintWriter writer = new PrintWriter(new FileWriter("output.txt"))) {
writer.println("Hello, World!");
writer.println("This is a print writer example.");
System.out.println("Data written to file.");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Data written to file.
Explanation:
- The
PrintWriterwrites formatted text with methods likeprintln().
Reading and Writing Binary Filesβ
For binary files (e.g., images, serialized objects), use FileInputStream and FileOutputStream.
Example: Reading a Binary Fileβ
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamExample {
public static void main(String[] args) {
try (FileInputStream inputStream = new FileInputStream("image.jpg")) {
int byteData;
while ((byteData = inputStream.read()) != -1) {
System.out.print(byteData + " ");
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Byte data of image.jpg
Explanation:
- The
FileInputStreamreads raw bytes from the file.
Example: Writing a Binary Fileβ
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamExample {
public static void main(String[] args) {
byte[] data = {72, 101, 108, 108, 111}; // ASCII values for "Hello"
try (FileOutputStream outputStream = new FileOutputStream("output.bin")) {
outputStream.write(data);
System.out.println("Binary data written to file.");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Binary data written to file.
Explanation:
- The
FileOutputStreamwrites raw bytes to the file.
Key Takeawaysβ
- Use
FileReaderfor simple character-by-character reading,BufferedReaderfor efficient line-by-line reading, andScannerfor parsing structured data. - Use
FileWriterfor simple writing,BufferedWriterfor efficient buffered writing, andPrintWriterfor formatted text output. - Use
FileInputStreamandFileOutputStreamfor reading and writing binary files. - Always use
try-with-resourcesto ensure that files are closed automatically after use.