← Back to Core Java Course | Chapter 12: File I/O | Lesson 5 of 9

Java FileReader & FileWriter

FileWriter writes text into a file and FileReader reads text back, like writing in and reading from a notebook. It works one character at a time.
Syntax
java
FileReader reader = new FileReader("file_name");
FileWriter writer = new FileWriter("file_name");
writer.write("text");
writer.close();

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

java
// Import java.io.FileWriter so it can be used by its short name
import java.io.FileWriter;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new FileWriter object and store it in writer
		FileWriter writer = new FileWriter("notes.txt");
		writer.write("Hello");
		writer.close();
		// Print a line to the console
		System.out.println("Written");
	}
}

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

java
// Import java.io.FileWriter so it can be used by its short name
import java.io.FileWriter;
// Import java.io.FileReader so it can be used by its short name
import java.io.FileReader;
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new FileWriter object and store it in writer
		FileWriter writer = new FileWriter("notes.txt");
		writer.write("Hi");
		writer.close();
		// Create a new FileReader object and store it in reader
		FileReader reader = new FileReader("notes.txt");
		int c;
		// Repeat while (c = reader.read()) != -1 is true
		while ((c = reader.read()) != -1) {
			// Print to the console without a newline
			System.out.print((char) c);
		}
		reader.close();
	}
}

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

java
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");
	}
}

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

java
// Import java.io.FileWriter so it can be used by its short name
import java.io.FileWriter;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Try code that might throw an exception
		try (FileWriter writer = new FileWriter("notes.txt")) {
			writer.write("Hello");
		// Handle IOException if it was thrown
		} catch (IOException e) {
			// Print a line to the console
			System.out.println("Handled safely: " + e.getMessage());
		}
	}
}

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

java
// Import java.io.FileWriter so it can be used by its short name
import java.io.FileWriter;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new StringBuilder object and store it in content
		StringBuilder content = new StringBuilder();
		// Loop: repeat while the condition holds
		for (int i = 1; i <= 3; i++) {
			content.append("Line ").append(i).append("\n");
		}
		// Try code that might throw an exception
		try (FileWriter writer = new FileWriter("notes.txt")) {
			writer.write(content.toString());
		}
		// Print a line to the console
		System.out.println("Written dynamically");
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using FileWriter without append mode, which overwrites existing text.
  2. Forgetting to close the writer, so the text may not be saved.
  3. Using FileReader for binary files, which corrupts the data.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.