---
# System prepended metadata

title: Java FileReader Class - Scaler Topics

---

---
title: Java FileReader Class - Scaler Topics
description: Learn about filereader in Java, along with its constructors, methods, examples, and code explanations on scaler Topics.
category: Java
author: Deepak Pandey
---
:::section{.abstract}

In Java, we have a FileReader class, which belongs to the **`java.io`** package. Basically, we use this FileReader class to read the data from any given file. The filereader in java reads the data in **form of characters** from the file. Hence, it is also known as a **`character-oriented class`**. In the form of bytes similar to the **`FileInputStream`** class, the data will be returned. The main purpose of this class is for **`file handling`** in Java. 

 In Java, we have a File class that belongs to the **`java.io`** package. Using the File class, we can perform numerous operations on files and directories.


The filereader in java either uses the **`platform’s default charset or the specified charset for decoding`** from bytes to characters. Let us learn about them in detail.
:::
:::section{.main}

## Java FileReader Class Declaration

Now, Let us learn how to declare a `filereader in java`.

**Declaration:**

```java
public class FileReader extends InputStreamReader
```

:::

:::section{.main}
## Constructors of FileReader Class

There are several constructors we can use to create the filereader in java; let us discuss them in detail.



| Constructor                                | Parameters                                                      | Description                                                                                                                                                                             |
| :------------------------------------------: | :---------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| **FileReader(File fileObject)**                  | It consists of only one parameter of type **File**           | With this constructor, a new FileReader instance is created by reading the file data given in the parameter. Here, we give the file to read. This constructor uses the default charset. |
| **FileReader(FileDescriptor files)**          | It consists of only one parameter of type **FileDescriptor**     | With this constructor, a new FileReader instance is created by reading the file description given in the parameter. This constructor uses the default charset.                         |
| **FileReader(File fileObject, Charset charset)** | It consists of two parameters, of type **File**and **Charset** | With this constructor, a new FileReader instance is created by reading the file data given in the parameter; here, we give the file to read. This constructor uses the specified charset given in the parameter.       |
| **FileReader(String fileName)**            | It consists of only one parameter of type **String**         | With this constructor, a new FileReader instance is created, with the given FileName to read; here, we give the name of the file to read. This constructor uses the default charset.     |
| **FileReader(String fileName, Charset charset)**                                           | It consists of two parameters, of type **String**, and **Charset**                                                                | With this constructor, a new FileReader instance is created by reading the file data given in the parameter, here we give the name of the file to read. This constructor uses the specified charset given in the parameter.                                                                                                                                                                                         |
:::
:::section{.main}
## Methods of FileReader Class

Let us discuss about all the methods of `filereader in java` in detail.



| Index | Method                                              | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| :-----: | :---------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| 1     | **read()**                                          | The read() method of the Java FileReader class reads the data of the file and returns a single character, and in case the stream is ended, it will return -1.                                                                                                                                                                                                                                                                                                                                                               |
| 2     | **read(char[] charBuffer, int offset, int length)** | The read(char[] charBuffer, int offset, int length) method of the Java FileReader class reads a stream of characters, and it stores those characters in a char array or Character Buffer given in the parameter. This method also consists of different parameters like **offset**, the position from which the FileReader will start reading, and the **length**, which is the total number of characters to be read. It returns plenty of characters to read, and if the stream ends, it will return -1. |
| 3     | **ready()**                                         | The ready() method of the Java FileReader class returns true if the stream is ready to be read. A stream can be read if its input buffer is not empty or blank.                                                                                                                                                                                                                                                                                                                                                     |
| 4     | **getEncoding()**                                   | The getEncoding() method of the Java FileReader class is used to return the character encoding's title, which is being used by the stream.                                                                                                                                                                                                                                                                                                                                                                                    |
| 5      | **close()**                                                    | The close() method of the Java FileReader class is used to close all the streams and liberate the system resources.                               |


:::

:::section{.main}
## Java FileReader Example 

In the example below, we read the `FileReader.txt` file using the `FileReader` class.

```java


//import the necessary packages
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;



//class begins here
public class FileReaderExample {
    
    //java main method
    public static void main(String args[])throws IOException {
        
        //create a file object using the File class
        File f1 = new File("C:\\FileReader.txt");
        
        //File gets created
        f1.createNewFile();
        
        // FileReader Object creation
        FileReader fread = new FileReader(f1);
        
        //create a character array c
        char [] ch = new char[100];
        
        //read the data to the array
        fread.read(ch);
        
        //print each character
        for(char i : ch)
        System.out.print(i);
        
        //close
        fread.close();
    }
}
```

**Output:**

```plaintext
Hello 
World 
This 
is 
a 
Scaler 
article . . . . . . . . . . 
```


:::

:::section{.summary}
## Conclusion


* The filereader in java belongs to the **`java.io`** package.
* The Java FileReader class extends the **`InputStreamReader`** class.
* The Java FileReader class uses either the platform’s default charset or the specified charset to decode from bytes to characters.
* The Java FileReader class is a public class that inherits or extends the **`InputStreamReader`** class.
* There are several constructors of FileReader class, like **`FileReader(File file)`**, **`FileReader(FileDescriptor fd)`**, **`FileReader(File file, Charset charset)`**, **`FileReader(String filename)`**, and **`FileReader(String filename, Charset charset)`**.
* There are different methods of FileReader class inJava, like **`read()`**, **`read(char[] charBuffer, int offset, int length)`**, **`ready()`**, **`getEncoding()`**, **`close()`**.
:::