Read from file using FileReader

The InputStreamReader class is the base class for all readers that perform conversion between byte and character streams. The FileReader class allows input from a file. It implements the abstract properties of Reader and provides additional constructors that ensure to open a file.

Syntax

import java.io.FileReader;

FileReader <fr_name> = new FileReader("<file_name.txt>");

Example

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

public class ReadingFromFile {
    public static void main(String[] args) throws IOException {
        FileReader fr;
        char[] target = new char[30];

        try {
            fr = new FileReader("<file_name.txt>");
            fr.read(target);
            System.out.println(target);
            fr.close();
        } catch (IOException e) {
            System.out.println("Error reading file");
            System.out.println(e.toString());
        }
    }
}
Output
Content from myFile.txt:
Lorem Ipsum Dolor Sit
Amet