Java FileReader & FileWriter
In this page:
Writing Characters with FileWriter
FileWriter writes character data to a file, automatically converting Java's internal char/String data into the platform's default text encoding as it writes.
Example: Writing Characters with FileWriter
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter writer = new FileWriter("notes.txt");
writer.write("Hello");
writer.close();
System.out.println("Written");
}
}
Login to try C/C++/Java/PHP code in the editor
Reading Characters with FileReader
FileReader reads character data back from a file the same way, converting bytes on disk into Java char values as it reads — pairing naturally with FileWriter for round-tripping text.
Example: Reading Characters with FileReader
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter writer = new FileWriter("notes.txt");
writer.write("Hi");
writer.close();
FileReader reader = new FileReader("notes.txt");
int c;
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
reader.close();
}
}
Login to try C/C++/Java/PHP code in the editor
Try-With-Resources cleanup
Using try-with-resources (try (FileWriter fw = new FileWriter(...)) {...}) guarantees the file is closed automatically when the block exits, even if an exception is thrown partway through.
Example: Try-With-Resources cleanup
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
try (FileWriter writer = new FileWriter("notes.txt")) { // auto-closed on exit
writer.write("Hello");
}
System.out.println("Closed automatically");
}
}
Login to try C/C++/Java/PHP code in the editor
File Handling Safety
Always wrap file operations in proper exception handling and resource closing — an unclosed file handle can leak resources, and a program that crashes mid-write can leave a file in a corrupted, half-written state.
Example: File Handling Safety
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("notes.txt")) {
writer.write("Hello");
} catch (IOException e) {
System.out.println("Handled safely: " + e.getMessage());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Text Manipulation
Because FileWriter writes character-by-character by default, combining it with a loop that builds and rewrites strings is a natural way to transform a file's text content on the fly.
Example: Dynamic Text Manipulation
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
StringBuilder content = new StringBuilder();
for (int i = 1; i <= 3; i++) {
content.append("Line ").append(i).append("\n");
}
try (FileWriter writer = new FileWriter("notes.txt")) {
writer.write(content.toString());
}
System.out.println("Written dynamically");
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: