77
Java FileReader Class
Java FileReader class is used to read data from the file. It returns data in byte format like FileInputStream class.
It is character-oriented class which is used for file handling in java.
Java FileReader class declaration
Let’s see the declaration for Java.io.FileReader class:
Constructors of FileReader class
Constructor | Description |
---|---|
FileReader(String file) | It gets filename in string. It opens the given file in read mode. If 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 file doesn’t exist, it throws FileNotFoundException. |
Methods of FileReader class
Method | Description |
---|---|
int read() | It is used to return a character in ASCII form. It returns -1 at the end of file. |
void close() | It is used to close the FileReader class. |
Java FileReader Example
In this example, we are reading the data from the text file testout.txt using Java FileReader class.
Here, we are assuming that you have following data in “testout.txt” file:
Welcome to Tutor Aspire.
Output:
Welcome to Tutor Aspire.
Next TopicJava BufferedWriter Class