FileReader in Java

FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.

FileReader extends InputStreamReader.

Note:

FileReader uses the default character encoding. If you want to specify a different character decoding scheme, use an InputStreamReader on a FileInputStream instead. The InputStreamReader lets you specify the character encoding scheme to use when reading the bytes in the underlying file.

Methods in FileReader

FileReader in Java

package com.java.io;

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

public class FileReaderExample {

    public static void main(String[] args) {
        Reader reader = null;
        try {
            reader = new FileReader("/Test/TestFolder/test-file.txt");
            int data;
            while ((data = reader.read()) != -1) {
                System.out.print((char) data);
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

}
Test Data

Leave a Reply

Your email address will not be published. Required fields are marked *