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).
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.
Constructors of FileReader
FileReader(String fileName) // Opens the specified file by name
FileReader(File file) // Opens a File object
read(): Reads a single character.read(char[] cbuf): Reads characters into a character array.close(): Closes the reader and releases resources.import java.io.FileReader;import java.io.IOException;public class FileReaderExample { public static void main(String[] args) { try (FileReader fr = new FileReader("example.txt")) { // Create FileReader for reading int data; while ((data = fr.read()) != -1) { // Read character by character System.out.print((char) data); // Cast byte to character and print } } catch (IOException e) { e.printStackTrace(); } }}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.System.out.print().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.
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
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.xxxxxxxxxximport java.io.FileWriter;import java.io.IOException;public class FileWriterExample { public static void main(String[] args) { try (FileWriter fw = new FileWriter("output.txt")) { // Create FileWriter for writing fw.write("Hello, FileWriter!\n"); // Write a string to the file fw.write("Writing more text data."); // Writing another line fw.flush(); // Ensure all data is written to the file } catch (IOException e) { e.printStackTrace(); } }}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.| Feature | FileReader / FileWriter | FileInputStream / FileOutputStream |
|---|---|---|
| Type | Character-based | Byte-based |
| Data Type | Designed for text (characters) | Designed for binary data (images, videos) |
| Read/Write Unit | Reads/writes characters (16-bit) | Reads/writes bytes (8-bit) |
| Use Case | Suitable for reading/writing text | Suitable for binary data (e.g., images) |
| Encoding | Converts bytes to characters and vice versa | No conversion (works with raw bytes) |