Guide to Java FileReader

Java FileReader class can be used to read data (stream of characters) from files. In this tutorial, we will learn about FileReader class, its constructors, methods and usages with the help of examples.

1. FileReader class

The FileReader class is:

  • meant for reading streams of characters.
  • part of java.io package.
  • extends InputStreamReader class.
  • implements Closeable, AutoCloseable and Readable interfaces.
  • if not provided, it uses the platform’s default charset.
  • uses a default buffer size for reading the files.

2. Creating FileReader

To use the FileReader in the application, we must first import it from package java.io using the import statement. For creating the instance of FileReader, use one of its constructors.

2.1. Creating FileReader using File Name

String fileName = "c:\temp\test.txt";
FileReader input = new FileReader(fileName);

2.2. Creating FileReader using File

File file = new File("c:\temp\test.txt");
FileReader input = new FileReader(file);

2.3. Specifying Character Encoding

Above both examples create the file reader instance with the default character encoding. To specify a different character encoding, we can pass the encoding information as Charset in the second argument to both constructors.

FileReader input = new FileReader(fileName, Charset.forName("UTF8"));

//or 

FileReader input = new FileReader(file, Charset.forName("UTF8"));

3. FileReader Examples

Let us see a few examples to read a file using the FileReader in Java.

Example 1: Reading Small Files in char[]

In the given example, we are reading a text file. The file contains 3 small hello world messages. Here we are attempting to read the file in single read() operation so make sure you create a sufficiently large char[] to store all the content on the file.

This should be used only for small text files.

String fileName = "demo.txt";

try(FileReader fileReader = new FileReader(fileName)) {
  
  char[] a = new char[2048];
  fileReader.read(a);   

  //verify content
  //System.out.println(new String(a));
}

Example 2: Reading a file one character at a time

In the given example, we are using the read() method which reads a single character from the file and returns it. When all the content of the file has been read, it returns -1 which indicates the end of the file.

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

public class FileReaderExample
{
	public static void main(String[] args) throws IOException
	{
		String fileName = "demo.txt";

		FileReader fileReader = new FileReader(fileName);

		try {
	    	 int i;
	         while((i = fileReader.read()) != -1) {
	         	System.out.print((char)i);
	         }
		} finally {
			fileReader.close();
		}
	}
}

Program output:

hello world 1
hello world 2
hello world 3

Example 3: Reading a file line by line using FileReader

FileReader does not directly support reading a file line by line. For this, we need to wrap the FileReader inside a BufferedReader instance which provides the method readLine().

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

public class FileReaderExample
{
	public static void main(String[] args) throws IOException
	{
		String fileName = "demo.txt";

		BufferedReader br = new BufferedReader(new FileReader(fileName));

		try {
		    String line;
		    while ((line = br.readLine()) != null) {
		       System.out.println(line);
		    }
		} finally {
		    br.close();
		}
	}
}

Program output:

hello world 1
hello world 2
hello world 3

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.