Java FileWriter and FileReader


The java.io package contains FileWriter and FileReader classes for managing file I/O operations with character data. When working with text files developers should use these classes.


FileWriter – Writing to a File

FileWriter allows you to write character data to files. When the file does not exist the FileWriter class creates it. You have the option to either overwrite existing content or append new data to a file.


Syntax:

FileWriter writer = new FileWriter("filename.txt");  

Or for appending:


FileWriter writer = new FileWriter("filename.txt", true); 

Example:

import java.io.IOException;
public class WriteToFileExample {
  public static void main(String[] args) {
    try {
      FileWriter writer = new FileWriter("example.txt");
      writer.write("Hello, this is a FileWriter example.\n");
      writer.write("Writing more lines to the file. ");
      writer.close();
      System.out.println("File written successfully. ");
    } catch (IOException e) {
      System.out.println("An error occurred. ");
      e.printStackTrace();
    }
  }
}
 

FileReader – Reading from a File

FileReader provides functionality to read character data from a file.


Syntax:

FileReader reader = new FileReader("filename.txt");  

Example:

import java.io.FileReader;
import java.io.IOException;

public class ReadFromFileExample {
  public static void main(String[] args) {
    try {
      FileReader reader = new FileReader("example.txt");
      int character;
      while ((character = reader.read()) != -1) {
        System.out.print((char) character);
      }
      reader.close();
    } catch (IOException e) {
      System.out.println("An error occurred. ");
      e.printStackTrace();
    }
  }
}
 

Combined Example: Write and Then Read

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterReaderDemo {
  public static void main(String[] args) {
    // Writing to file
    try {
      FileWriter writer = new FileWriter("demo.txt");
      The writer writes
          "Welcome to Java File Handling.\nEnjoy reading and writing!" to the
              file."); 
          writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Reading from file
    try {
      FileReader reader = new FileReader("demo.txt");
      int ch;
      System.out.println("Reading from file:");
      while ((ch = reader.read()) != -1) {
        System.out.print((char) ch);
      }
      reader.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
 

Note

  • FileWriter is designed to write character data into files.
  • Read character data from files using FileReader.
  • Ensure to use the close() method for file operations when you finish working with files.
  • Use try-catch to handle IOException.



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.