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.

FileReader:

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:

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.
Print Friendly, PDF & Email
FileWriter in java
FileInputStream and FileOutputStream in java
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

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 […]