English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Arrays

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collections

Java Set Collections

Java Input Output (I/O)/O)

Java Reader/Writer

Java Other Topics

Java FileReader Class

In this tutorial, we will learn about Java FileReader and its methods with the help of examples.

The FileReader class in the java.io package can be used to read data from a file (character by character).

It inherits from the InputSreamReader class.

Before learning FileReader, make sure you understandJava File.

Create a FileReader

To create a file reader, we must first import the java.io.FileReader package. After importing the package, here is the method to create a file reader.

1.Using the filename

FileReader input = new FileReader(String name);

Here, we create a file reader that will be linked to the specified filename.

2.Using the file object

FileReader input = new FileReader(File fileObj);

Here, we create a file reader that will be linked to the file specified by the file object.

In the above example, the data in the file is stored using some default character encoding.

However, due to Java 11We can also specify the type of character encoding in the file (UTF-8OrUTF-16)

FileReader input = new FileReader(String file, Charset cs);

Here, we use the Charset class to specify the character encoding of the file reader.

FileReader methods

The FileReader class provides implementations of different methods that exist in the Reader class.

The read() method

  • read() - Read a single character from the reader

  • read(char[] array) - Read characters from the reader and store them in the specified array

  • read(char[] array, int start, int length) - Reads characters equal to length from the reader and stores them starting at position start in the specified array.

For example, suppose we have a file namedinput.txtwhich contains the following content.

This is a line of text inside the file.

Let's try to use the FileReader to read the file.

import java.io.FileReader;
class Main {
  public static void main(String[] args) {
    //Create a character array
    char[] array = new char[100];
    try {
      //Create a reader using FileReader
      FileReader input = new FileReader("input.txt");
      //Read characters
      input.read(array);
      System.out.println("Data in the file: ");
      System.out.println(array);
      // Close the reader
      input.close();
    }
    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

Data in the file:
This is a line of text inside the file.

In the above example, we created a file reader named input. The file reader is associated with the fileinput.txtLink.

FileInputStream input = new FileInputStream("input.txt");

To read data from the file, we use the read() method.

The getEncoding() method

The getEncoding() method can be used to get the encoding type used to store data in the file. For example,

import java.io.FileReader;
import java.nio.charset.Charset;
class Main {
  public static void main(String[] args) {
    try {
      //Create a FileReader with default encoding
      FileReader input1 = new FileReader("input.txt");
      //Create a FileReader with specified encoding
      FileReader input2 = new FileReader("input.txt", Charset.forName("UTF8"));
      //Returns the character encoding of the file reader
      System.out.println("input1The character encoding: " + input1.getEncoding());
      System.out.println("input2The character encoding: " + input2.getEncoding());
      //Close the reader
      input1.close();
      input2.close();
    }
    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

input1character encoding: Cp1252
input2character encoding: UTF8

In the above example, we created2file readers, named input1and input2.

  • input1No character encoding is specified. Therefore, the getEncoding() method returns the default character encoding.

  • input2Specify Character EncodingUTF8. Therefore, the getEncoding() method returns the specified character encoding.

Note: We have specified the type of character encoding using the Charset.forName() method.

close() Method

To close the file reader, we can use the close() method. After calling the close() method, we will not be able to use the reader to read data.

Other Methods of FileReader

MethodDescription
ready()Check if the file reader is ready to read
mark()Mark the position of the data read in the file reader
reset()Reset the reader to the position of the mark set in the reader