in , ,

Java FileReader Class

Java FileReader Class
Java FileReader Class

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

The FileReader class of the java.io package can be used to understand data (in characters) from files.

It extends the InputSreamReader class.


Make a FileReader

To make a file reader, we should import the java.io.FileReader package first. When we import the package, here is the means by which we can make the file reader.

1. Using the name of the file

FileReader input = new FileReader(String name);

Here, we have made a record reader that will be connected to the file indicated by the name.

2. Using an object of the file

FileReader input = new FileReader(File fileObj);

Here, we have made a record reader that will be connected to the file indicated by the object of the record.

In the above example, the data in the record are stored using some default character encoding.

In any case, since Java 11 we can determine the sort of character encoding (UTF-8 or UTF-16) in the file too.

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

Here, we have used the Charset class to indicate the character encoding of the record reader.


Methods of FileReader

The FileReader class gives executions to various techniques present in the Reader class.

read() Method

  • read() – reads a single character from the reader
    read(char[] array) – reads the characters from the reader and stores in the specified array
  • read(char[] cluster, int start, int length) – reads the number of characters equivalent to length from the reader and stores in the predefined exhibit beginning from the position start

For instance, assume we have a record named input.txt with the accompanying substance.

This is a line of text inside the file.

Let’s try to read the file using FileReader.

import java.io.FileReader;

class Main {
  public static void main(String[] args) {

    // Creates an array of character
    char[] array = new char[100];

    try {
      // Creates a reader using the FileReader
      FileReader input = new FileReader("input.txt");

      // Reads characters
      input.read(array);
      System.out.println("Data in the file: ");
      System.out.println(array);

      // Closes the reader
      input.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output

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

In the above example, we have made a record reader named input. The file reader is connected with the record input.txt.

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

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


getEncoding() Method

The getEncoding() technique can be used to get the type of encoding that is used to store data in the record. For instance,

import java.io.FileReader;
import java.nio.charset.Charset;

class Main {
  public static void main(String[] args) {

    try {
      // Creates a FileReader with default encoding
      FileReader input1 = new FileReader("input.txt");

      // Creates a FileReader specifying the encoding
      FileReader input2 = new FileReader("input.txt", Charset.forName("UTF8"));

      // Returns the character encoding of the file reader
      System.out.println("Character encoding of input1: " + input1.getEncoding());
      System.out.println("Character encoding of input2: " + input2.getEncoding());

      // Closes the reader
      input1.close();
      input2.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output

The character encoding of input1: Cp1252
The character encoding of input2: UTF8

In the above example, we have made 2 record peruser named input1 and input2.

  • input1 doesn’t indicate the character encoding. Consequently the getEncoding() technique returns the default character encoding.
  • input2 determines the character encoding, UTF8. Subsequently the getEncoding() strategy returns the predefined character encoding.

Note: We have used the Charset.forName() technique to determine the type of character encoding. To find out additional, visit Java Charset (official Java documentation).


close() Method

To close the record reader, we can use the close() strategy. When the close() strategy is called, we can’t use the reader to read the data.


Other Methods of FileReader

MethodDescription
ready()checks if the file reader is ready to be read
mark()mark the position in file reader up to which data has been read
reset()returns the control to the point in the reader where the mark was set

To find out additional, visit Java FileReader (official Java documentation).


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

One Comment

Leave a Reply

Leave a Reply

Java OutputStreamWriter Class

Java OutputStreamWriter Class

meet-now

Step by step instructions to Use Meet Now: Skype’s Free Zoom Alternative