Handling text files is a common task in Java programming. This guide will cover how to create, write to, and read from text files using Java. We will use Java's built-in classes and methods to achieve this. You'll also learn about file management techniques to handle files efficiently.
1. Creating and Writing to Text Files
Java provides several ways to create and write to text files. We will use the `FileWriter` and `BufferedWriter` classes for this purpose. The `FileWriter` class is used for writing character data to a file, while `BufferedWriter` provides buffering to improve performance.
1.1 FileWriter Class
The `FileWriter` class is a basic way to write text to a file. It writes characters to a file using the default character encoding.
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("example.txt")) {
writer.write("Hello, World!");
writer.write(System.lineSeparator()); // Adds a new line
writer.write("Java File I/O Example");
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.2 BufferedWriter Class
The `BufferedWriter` class wraps around a `FileWriter` to provide buffering. This is more efficient when writing large amounts of data.
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("example.txt"))) {
writer.write("Hello, World!");
writer.newLine(); // Adds a new line
writer.write("Java BufferedWriter Example");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. Reading from Text Files
Reading from text files can be accomplished using the `FileReader` and `BufferedReader` classes. `FileReader` reads characters from a file, and `BufferedReader` adds buffering for efficient reading.
2.1 FileReader Class
The `FileReader` class reads the file character by character. This is suitable for simple use cases.
import java.io.FileReader;
import java.io.IOException;
public class FileReaderExample {
public static void main(String[] args) {
try (FileReader reader = new FileReader("example.txt")) {
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.2 BufferedReader Class
The `BufferedReader` class reads text efficiently by buffering characters. It also provides methods to read lines of text.
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("example.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. Creating a Custom File Writer Class
For more control over file writing, you can create a custom class that encapsulates file writing functionality. This class will use `FileWriter` and `BufferedWriter` to provide an easy-to-use interface for writing text files.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CustomFileWriter {
private BufferedWriter writer;
// Constructor
public CustomFileWriter(String fileName) throws IOException {
writer = new BufferedWriter(new FileWriter(fileName));
}
// Method to write a line of text
public void writeLine(String line) throws IOException {
writer.write(line);
writer.newLine();
}
// Method to close the writer
public void close() throws IOException {
writer.close();
}
public static void main(String[] args) {
try {
CustomFileWriter fileWriter = new CustomFileWriter("custom_example.txt");
fileWriter.writeLine("Hello from CustomFileWriter!");
fileWriter.writeLine("This is another line.");
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. Handling Exceptions
When working with file I/O, it's crucial to handle exceptions properly. `IOException` is the primary exception to handle, which indicates issues with file operations such as reading from or writing to a file.
- Try-with-Resources: This construct ensures that resources are closed automatically, reducing the risk of resource leaks.
- Catch Block: Handles exceptions and provides a way to manage errors gracefully.
Comments
Post a Comment