Java Programming Handbook

    Java FileReader and FileWriter Classes

    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.

    1. Class Hierarchy#

    java.lang.Object ↳ java.io.Reader ↳ java.io.FileReader java.lang.Object ↳ java.io.Writer ↳ java.io.FileWriter
    • FileReader extends Reader
    • FileWriter extends Writer

    2. FileReader Class#

    ● Constructors#

    FileReader(String fileName) FileReader(File file)

    These constructors open a file for reading. If the file doesn't exist, a FileNotFoundException is thrown.

    ● Example: Reading from a file using FileReader#

    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(); } } }

    Output (if example.txt contains "Hello World"):#

    Hello World

    ● int read()#

    Reads a single character.

    ● int read(char[] cbuf, int offset, int length)#

    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(); } } }

    Output:#

    Characters read: Hello World

    ● void close()#

    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(); } } }

    Output:#

    Reader opened. Reader closed.

    3. FileWriter Class#

    ● Constructors#

    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.

    ● Example: Writing to a file using FileWriter#

    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(); } } }

    Output:#

    Data written successfully.

    File output.txt will contain:

    Hello Java

    ● void write(int c)#

    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(); } } }

    Output:#

    Single character written.

    File content:

    A

    ● void write(char[] cbuf, int off, int len)#

    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(); } } }

    Output:#

    Char array written.

    ● void write(String str, int off, int len)#

    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(); } } }

    Output:#

    String written with offset.

    File content:

    FileWriter Demo

    ● void flush()#

    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(); } } }

    Output:#

    Data flushed.

    ● void close()#

    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(); } } }

    Output:#

    Writer closed.

    Conclusion#

    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.

    Last updated on Apr 09, 2025