FileReader Java – How to use FileReader in Java and what to import for FileReader class and methods with Java FileReader Examples



File FileReader Java

FileReader in Java

FileReader in Java is a class that we use to read data from a file. It is character-oriented and returns data in the form of bytes. This class is part of the java.io package and extends the InputStreamReader class.

FileReader in Java

Java FileReader constructors

The FileReader class supports two types of constructors:

FileReader(String file): Opens the specified file using the filename as a string.

FileReader(File f): Opens the specified file using the filename as a File Object.

Both the constructors throw FileNotFoundException if the specified file is not present.

Java FileReader methods

Below is the list of methods that the FileReader class supports for reading data from the file.

MethodsDescription
void close()Closes the file reader object
void mark(int readLimit)Marks the current position in the stream
boolean markSupported()Checks whether the stream supports mark() and reset() methods.
int read()Reads a single character
int read(char[] c)Reads an array of characters
int read(CharBuffer target)Attempts to read the characters into the specified character buffer
int read(char[] c, int offset, int len)Reads the specified length of characters into the array starting from the specified offset position
boolean ready()Checks if the stream is ready to read
long skip(long n)Skips or discards the characters during the read operation
String toString()Returns a string representation of the file object
long transferTo(Writer out)Reads all the characters and writes it in the same order to the output stream

Java FileReader Examples

Read a character using the FileReader

The read() method of the FileReader class in Java reads a single character at a time from the file. Below is the example that shows to read a single character. The InputFile contains the test “FileReader example”. Hence the first output is ‘F’ and the second output is ‘i’.

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

public class ReadFileDemo {

  public static void main(String[] args) throws IOException {
    FileReader fr = new FileReader("InputFile.txt");
    
    char ch = (char)fr.read();
    System.out.println("Single character read: " + ch);
    System.out.println("Single character read: " + (char)fr.read());
    
    fr.close();
  }

}
Single character read: F
Single character read: i

Read an array of characters using FileReader

In the below example, we read the full content of the file using the read() method of Java FileReader. We can achieve this by calling the read method within a while loop so that it reads individual characters until it reaches the end of the file. When there are no characters to read, the read() method returns -1.

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

public class ReadFileDemo {

  public static void main(String[] args) throws IOException {
    FileReader fr = new FileReader("InputFile.txt");
    
    //Read all content
    int i;
    System.out.println("File content:");
    while((i=fr.read())!=-1)
      System.out.print((char)i);
    
    fr.close();
  }

}
File content:
FileReader example

Read the specific length of characters

In the below example, we can how to read a specific length of characters(which is 5 in this case) from the file using the read() method. We can specify the starting position to read along with the number of characters to be read. Hence it prints the output with 5 characters which is ‘FileR’ where the input file content is “FileReader example”.

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

public class ReadFileDemo {

  public static void main(String[] args) throws IOException {
    FileReader fr = new FileReader("InputFile.txt");
    
    //Read specific length of characters
    char[] ch = new char[10];
    int i = fr.read(ch, 0, 5);
    System.out.println("Number of characters read: " + i);
    
    for(char c : ch) {
      System.out.print(c);
    }
    
    fr.close();
  }

}
Number of characters read: 5
FileR

Skip and read characters using FileReader

The skip() method of the Java FileReader class skips the specified number of characters and reads the remaining characters. In this example, we skip the first 4 characters and then read from the 5th character until the end of the file. Hence when the InputFile contains “FileReader example” as the content, it prints only “Reader example” as the output.

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

public class ReadFileDemo {

  public static void main(String[] args) throws IOException {
    FileReader fr = new FileReader("InputFile.txt");
    
    //Skip and read
    fr.skip(4);
    System.out.println("Content after skipping 4 characters:");
    int i;
    while((i=fr.read())!=-1)
      System.out.print((char)i);
    
    fr.close();
  }

}
Content after skipping 4 characters:
Reader example

 

Reference

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions
Core Java Interview Questions