FileReader in java

In previous post, we have seen FileWriter. In this post, we will see how to read data from a text file with the help of  FileReader in java.

FileReader is a  character based classe unlike FileInputStream which is used for reading byte based data.In other words,  we should use FileReader class when the data is in textual form.

Let’s understand it with example.


It reads data as a stream of characters from a file.

Constructor of FileReader:

Constrctor Description
FileReader(String file) Filename is passed as String. File is opened in read mode and if file does not exist ,throws FileNotFoundException.
FileReader(File file)
Filename is passed as File Object. File is opened in read mode and if file does not exist ,throws FileNotFoundException.

Example of FileReader:


public class FileReaderEx {
	public static void main(String[] args){		
	  FileReader freader;
			try {
				freader = new FileReader("Sample.txt");
				  int i;  
			} catch (Exception e) {
Output: FileWriter example in testingpool

FileReader methods:

  1. public int read() : It returns a character in ASCII form and returns -1 at the end of file.
  2. public void close() : It closes FileReader.

In next post, we will see FileInputStream and FileOutputStream.

Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

2 Responses

  1. August 8, 2015

    […] the next post, we will see FileReader which is used for reading data from a text […]

  2. August 8, 2015

    […] the previous post, We have seen FileReader which is used to read the data from text file. In this post, we will look at FileInputStream and […]