Signup/Sign In
PUBLISHED ON: FEBRUARY 24, 2021

Java FileReader Class

In this tutorial, we will learn FileReader class in Java. This class helps to read the data from the file as a stream, of characters. Java FileReader is a subclass of the Java Reader class, so it has many of the same methods.

Syntax

This is the syntax declaration of FileReader class. This class extends the InputStreamReader class.

public class FileReader extends InputStreamReader  

Constructors of FileReader Class

Constructor Description
FileReader(String file) It gets the filename in a string. It opens the given file in read mode. If the file doesn't exist, it throws FileNotFoundException.
FileReader(File file) It gets filename in file instance. It opens the given file in read mode. If the file doesn't exist, it throws FileNotFoundException.

Methods of FileReader Class

Method Description
int read() This method is used to return a character in ASCII form. It returns -1 at the end of the file.
void close() This method is used to close the FileReader class.

Example of read() method in FileReader Class

In the following example, we are reading the data from the file. Firstly we created a FileReader after that using the read() method we are reading each character and print it on the console.

package studytonight;
import java.io.FileReader;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{   		
			FileReader fileReader=new FileReader("E:\\studytonight\\output.txt");    
			int i;    
			while((i=fileReader.read())!=-1)    
				System.out.print((char)i);    
			fileReader.close();    
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Welcome to Studytonight

output.txt

Welcome to Studytonight

Example of getEncoding() in FileReader

In the following example, we are implementing getEncoding() method that can be used to get the type of encoding that is used to store data in the file. We can see in the output of this example we are getting UTF8 as a character encoding of this fileReader2. If we do not specify in that case it will display default encoding.

package studytonight;
import java.io.FileReader;
import java.nio.charset.Charset;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{   		
			FileReader fileReader1 = new FileReader("E:\\studytonight\\output.txt");
			FileReader fileReader2 = new FileReader("E:\\studytonight\\output.txt", Charset.forName("UTF8"));
			System.out.println("Character encoding of fileReader1: " + fileReader1.getEncoding());
			System.out.println("Character encoding of fileReader2: " + fileReader2.getEncoding());
			fileReader1.close();
			fileReader2.close();
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Character encoding of fileReader1: Cp1252
Character encoding of fileReader2: UTF8

Conclusion:

In this tutorial, we learned about FileReader class. This class makes it possible to read the contents of a file as a stream of characters. getEncoding() method of this class is used to check the encoding of files.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.