Working with files is a basic but important part of Java programming. Whether you're storing data, reading logs, or processing text, Java provides several classes to make file handling simple and efficient.
In this post, let’s walk through some commonly used classes:
File
FileReader
FileWriter
BufferedReader
BufferedWriter
1. File Class
The File class is used to represent a file or directory path. It doesn’t actually read or write data — it just gives information about the file.
Example:
import java.io.File;
public class FileExample {
public static void main(String[] args) {
File file = new File("example.txt");
if (file.exists()) {
System.out.println("File exists");
System.out.println("File name: " + file.getName());
System.out.println("Path: " + file.getAbsolutePath());
} else {
System.out.println("File does not exist");
}
}
}
2. FileReader
FileReader is used to read data from a file character by character. It’s simple but not the most efficient for large files.
Example:
import java.io.FileReader;
import java.io.IOException;
public class FileReaderExample {
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("example.txt");
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
reader.close();
}
}
3. FileWriter
FileWriter is used to write text into a file. If the file doesn’t exist, it will be created automatically.
Example:
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
public static void main(String[] args) throws IOException {
FileWriter writer = new FileWriter("example.txt");
writer.write("Hello, this is a file writing example.");
writer.close();
}
}
4. BufferedReader
BufferedReader is used along with FileReader to read text efficiently. It reads larger chunks(a group of characters) instead of one character at a time.
Example:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("example.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
5. BufferedWriter
BufferedWriter is used with FileWriter to write data efficiently. It reduces the number of write operations.
Example:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("example.txt"));
bw.write("Hello, this is written using BufferedWriter.");
bw.newLine();
bw.write("Writing another line.");
bw.close();
}
}
Why Use Buffered Classes?
Without buffering:
Data is read/written one character at a time → slow
With buffering:
Data is handled in chunks(A fixed-size block of data) → faster and more efficient
Final Thoughts
Use File to manage file paths and properties
Use FileReader and FileWriter for basic operations
Use BufferedReader and BufferedWriter for better performance
In real-world applications, buffered classes are preferred because they improve speed and reduce system load.
Top comments (0)