FileReader and FileWriter in Java

In Java, FileReader and FileWriter are character-based classes used for reading from and writing to files. They are part of the java.io package and are specifically designed to handle text files (i.e., files that contain human-readable characters).

Both FileReader and FileWriter deal with text data, which means they handle characters instead of bytes, unlike FileInputStream and FileOutputStream, which are designed for byte-based data (such as images, videos, and binary files).


What is FileReader?

FileReader is a character-based class that is used to read data from text files. It reads characters from a file and converts them to their corresponding Unicode characters, making it suitable for handling text data.

How Does FileReader Work?

  • FileReader works by reading characters from a file. It reads the raw bytes from the file and converts them into characters based on the platform's default character encoding (usually UTF-8 or ISO-8859-1).
  • Since it's character-based, it's ideal for reading text files.

Constructors of FileReader

FileReader(String fileName)  // Opens the specified file by name
FileReader(File file)        // Opens a File object

Key Methods of FileReader

  • read(): Reads a single character.
  • read(char[] cbuf): Reads characters into a character array.
  • close(): Closes the reader and releases resources.
Code Example

Explanation:

  • FileReader opens the file example.txt for reading.
  • fr.read() reads one character at a time and returns the character's integer value. When the end of the file is reached, it returns -1.
  • The characters are printed to the console using System.out.print().

What is FileWriter?

FileWriter is a character-based class that is used to write data to text files. It writes characters to a file, converting them into the platform's default encoding (typically UTF-8). Like FileReader, it’s designed specifically for handling text data.

How Does FileWriter Work?

  • FileWriter works by writing characters to a file. It converts the characters into bytes using the platform's default character encoding and writes the bytes to the file.
  • It is commonly used for writing text files, such as log files, configuration files, or simple plain text documents.

Constructors of FileWriter

FileWriter(String fileName) // Creates a new file or overwrites if it exists
FileWriter(File file) // Writes to the specified File object
FileWriter(String fileName, boolean append) // Writes to the file in append mode if true

Key Methods of FileWriter

  • write(int c): Writes a single character.
  • write(char[] cbuf): Writes an array of characters.
  • write(String str): Writes a string to the file.
  • flush(): Flushes the writer, ensuring that all data in the buffer is written to the file.
  • close(): Closes the writer and releases resources.
Code Example

Explanation:

  • FileWriter opens or creates output.txt for writing.
  • fw.write() writes the string "Hello, FileWriter!" to the file, followed by "Writing more text data.".
  • fw.flush() ensures that any remaining data in the buffer is written to the file before closing.

Difference Between FileReader/FileWriter and FileInputStream/FileOutputStream

FeatureFileReader / FileWriterFileInputStream / FileOutputStream
TypeCharacter-basedByte-based
Data TypeDesigned for text (characters)Designed for binary data (images, videos)
Read/Write UnitReads/writes characters (16-bit)Reads/writes bytes (8-bit)
Use CaseSuitable for reading/writing textSuitable for binary data (e.g., images)
EncodingConverts bytes to characters and vice versaNo conversion (works with raw bytes)
  • FileReader/FileWriter are specifically designed for character-based data like text files. They handle character encoding (UTF-8, etc.).
  • FileInputStream/FileOutputStream handle byte-based data, making them more suitable for handling binary files like images or videos.