When working with files in Java, FileReader
and FileWriter
provide a convenient way to read and write text data using character streams. These classes are specifically designed for handling character files.
java.lang.Object
↳ java.io.Reader
↳ java.io.FileReader
java.lang.Object
↳ java.io.Writer
↳ java.io.FileWriter
FileReader
extends Reader
FileWriter
extends Writer
FileReader(String fileName)
FileReader(File file)
These constructors open a file for reading. If the file doesn't exist, a FileNotFoundException
is thrown.
import java.io.*;
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); // Print each character one by one
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Reads a single character.
Reads characters into a portion of an array.
import java.io.*;
public class FileReaderReadArray {
public static void main(String[] args) {
char[] buffer = new char[100];
try (FileReader reader = new FileReader("example.txt")) {
int charsRead = reader.read(buffer, 0, buffer.length);
System.out.println("Characters read: " + new String(buffer, 0, charsRead));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Characters read: Hello World
Closes the reader. Automatically handled with try-with-resources.
import java.io.*;
public class FileReaderCloseExample {
public static void main(String[] args) {
FileReader reader = null;
try {
reader = new FileReader("example.txt");
System.out.println("Reader opened.");
reader.read();
reader.close(); // manually closing
System.out.println("Reader closed.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Reader opened.
Reader closed.
FileWriter(String fileName)
FileWriter(String fileName, boolean append)
FileWriter(File file)
FileWriter(File file, boolean append)
These constructors create a file if it doesn't exist and optionally allow appending.
import java.io.*;
public class FileWriterExample {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("output.txt")) {
writer.write("Hello Java");
System.out.println("Data written successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Data written successfully.
File output.txt
will contain:
Writes a single character.
import java.io.*;
public class FileWriterWriteChar {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("output.txt")) {
writer.write(65); // Writes 'A'
System.out.println("Single character written.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Single character written.
File content:
Writes a portion of a character array.
import java.io.*;
public class FileWriterCharArray {
public static void main(String[] args) {
char[] data = "Welcome to Java".toCharArray();
try (FileWriter writer = new FileWriter("output.txt")) {
writer.write(data, 0, data.length);
System.out.println("Char array written.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Writes a portion of a string.
import java.io.*;
public class FileWriterWriteString {
public static void main(String[] args) {
String msg = "Java FileWriter Demo";
try (FileWriter writer = new FileWriter("output.txt")) {
writer.write(msg, 5, msg.length() - 5); // Write "FileWriter Demo"
System.out.println("String written with offset.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
String written with offset.
File content:
Flushes the stream — forces any buffered output to be written.
import java.io.*;
public class FileWriterFlushExample {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("output.txt")) {
writer.write("Flush Example");
writer.flush(); // Force write
System.out.println("Data flushed.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Closes the writer. Automatically done by try-with-resources.
import java.io.*;
public class FileWriterCloseExample {
public static void main(String[] args) {
FileWriter writer = null;
try {
writer = new FileWriter("output.txt");
writer.write("Close Example");
writer.close();
System.out.println("Writer closed.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
In this blog, we explored the FileReader
and FileWriter
classes which are ideal for reading and writing character-based files. We demonstrated each method with practical examples and outputs to make the concepts beginner-friendly.
Next, we’ll dive into BufferedReader and BufferedWriter classes for more efficient file handling using buffers.
Subscribe to our newsletter
Read articles from Coding Shuttle directly inside your inbox. Subscribe to the newsletter, and don't miss out.