Reading and Writing Files in Java using FileReader, FileWriter, and BufferedReader
Course Title: Comprehensive Java Programming: From Basics to Advanced Concepts Section Title: File I/O and Working with External Data Topic: Reading and writing files using FileReader, FileWriter, and BufferedReader
In this topic, we'll explore the basics of file input/output operations in Java, focusing on reading and writing text files using FileReader, FileWriter, and BufferedReader classes. We'll delve into the details of each class, their methods, and usage examples to ensure you grasp these essential concepts.
Introduction to FileReader
The FileReader class in Java is used to read character files. It's a subclass of InputStreamReader, which is a concrete subclass of Reader. FileReader creates a Reader that reads from the specified file.
Here's a basic example of using FileReader to read a text file:
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (FileReader fr = new FileReader("test.txt")) {
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}
In this example, we are reading a file called test.txt
and printing its contents to the console.
Introduction to FileWriter
The FileWriter class in Java is used to write to character files. It's a subclass of OutputStreamWriter, which is a concrete subclass of Writer. FileWriter creates a Writer that writes to the specified file.
Here's a basic example of using FileWriter to write to a text file:
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (FileWriter fw = new FileWriter("test.txt")) {
fw.write("Hello, World!");
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}
}
In this example, we are writing "Hello, World!" to a file called test.txt
.
Introduction to BufferedReader
The BufferedReader class in Java reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. A BufferedReader is always used when reading text files.
Here's an example of using BufferedReader to read a text file:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}
In this example, we are reading a file called test.txt
and printing its contents to the console, line by line.
Best Practices
- Always close your files after you're done using them to prevent resource leaks. Java 7 introduced a try-with-resources statement, which automatically closes your files.
- Use a BufferedReader for faster and more efficient reading of text files.
- Make sure to handle exceptions properly to prevent your program from crashing when there are errors reading or writing files.
Common methods and constructors
Here are some of the most commonly used methods and constructors of FileReader, FileWriter, and BufferedReader:
- FileReader:
FileReader(File file)
FileReader(String fileName)
- FileWriter:
FileWriter(File file)
FileWriter(File file, boolean append)
FileWriter(String fileName)
FileWriter(String fileName, boolean append)
- BufferedReader:
BufferedReader(Reader in)
BufferedReader(Reader in, int sz)
Conclusion
In conclusion, this topic has covered the basics of reading and writing files using FileReader, FileWriter, and BufferedReader classes in Java. You have learned how to use these classes to read and write to text files, and also how to use a try-with-resources statement to automatically close your files.
Further Reading
For more information on reading and writing files in Java, you can refer to the official Oracle documentation:
What to Expect Next
In the next topic, we will cover 'Working with data formats: Text, CSV, and JSON'. We will learn how to work with these data formats using Java, including reading and writing data to CSV and JSON files.
Do you have any questions or need help?
Please feel free to leave a comment or ask for help if you have any questions or need further clarification on any of the concepts covered in this topic.
Images

Comments