Introduction to FileReader in Java
In Java, FileReader is a class that is conveniently used for reading characters or data from files. Here, the data will be returned in the form of bytes similar to the FileInputStream class. Moreover, the constructors of the FileReader class consider that the default size of byte-buffer and character encoding is appropriate. For mentioning these values, an InputStreamReader or FileInputStream has to be constructed. This class can be instantiated from the package java.io.FileReader. Since the FileReader class is considered as a subclass of JavaReader class, most of the methods are common in them. In the following sections, we will see several aspects of the FileReader class in Java.
Declaration of FileReader:
First, let us see how to declare FileReader in Java.
public class FileReader extends InputStreamReader
Constructors
FileReader in Java has three constructors. They are:
- FileReader(File file): A new FileReader will be created, given f is the file to read from.
- FileReader(FileDescriptor fdr): A new FileReader will be created; given filedescriptor fdr has to be read from.
- FileReader(String f): A new FileReader will be created; given f is the file to read from.
Methods
FileReader class in Java inherits several methods from the classes InputStreamReader, Reader, and Object. Let us see what they are and what functionality each one of them has.
Methods that are inherited from java.io.InputStreamReader
- close(): The stream gets closed and all the system resources associated with it will be released.
- getEncoding(): Character encoding name will be returned which is used by the stream.
- read(char[] cbuf, int offset, int length): Characters will be read into the array portion.
- read(): The character will be read
- ready(): Checks if the stream is ready to be read.
Methods that are inherited from java.io.Reader
Some methods such as read and ready are available in this class also.
- read(char[] cbuf, int offset, int length): Characters will be read into the array portion.
- read(): The character will be read.
- ready(): Checks if the stream is ready to be read.
- markSupported(): Tells whether the function mark() is supported in the stream.
- read(CharBuffertarget): Try to read the characters into the target buffer mentioned.
- reset(): The stream will be reset.
- skip(): Characters will be skipped.
Methods that are inherited from java.lang.Object
- clone(): A copy of the object will be created and returned.
- equals(Object obj): Checks, whether the object mentioned, is equal to another object.
- finalize(): When an object has no longer any references, the garbage collector will be called.
- getClass(): Runtime class of the object will be created.
- hashCode(): Hashcode value of the object will be returned.
- notify(): A single thread will be wakening which was waiting for the current object’s monitor.
- notifyAll(): All threads will waken which was waiting for the current object’s monitor.
- toString(): Object’s string representation will be returned.
- wait(): The current thread will be waiting till any other thread calls the method to notify() or notifyAll() for the object
- wait(long timeout): The current thread will be waiting till any other thread calls the method to notify() or notifyAll() for the object or the amount of time mentioned gets elapsed.
- wait(long timeout, int nanosc): The current thread will be waiting till any other thread calls the method to notify() or notifyAll() for the object or the amount of time mentioned gets elapsed. In addition to that, it also helps in the situation where any other thread tries to interrupt the thread.
Examples of FileReader in Java
In order to understand FileReader in Java, it is good to create programs. So, let us look into some of the simple programs of FileReader in Java.
Example #1 – To implement FileReader
Code:
//Java program to implement FileReader class
//import the necessary packages
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//class begins here
public class FileReaderExample {
//main method
public static void main(String args[])throws IOException {
//create a file object in the location mentioned
File f = new File("D:\\EduCBA.txt");
//File gets created
f.createNewFile();
// FileWriter Object creation
FileWriter fw = new FileWriter(f);
// Write the text mentioned below to the file
fw.write("I\n am\n happy\n with\n my\n life\n");
fw.flush();
fw.close();
// FileReader Object creation
FileReader frd = new FileReader(f);
//create a character array c
char [] c = new char[50];
//read the data to the array
frd.read(c);
//print each characters
for(char ca : c)
System.out.print(ca);
//close
frd.close();
}
}
Output:
In this program, a file will be created in the specified location with the text mentioned. After that, an object of file reader will be created so that the data available in the text file can be read. Once it is done, a character array will be created and each character in the text file will be read one by one and gets printed.
Example #2 – To find the encoding of text given in a file
Code:
//Java program to implement FileReader class
//import the necessary packages
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//class begins here
public class FileReaderExample {
//main method
public static void main(String args[])throws IOException {
//create a file object in the location mentioned
File f = new File("D:\\EduCBA.txt");
//File gets created
f.createNewFile();
// FileWriter Object creation
FileWriter fw = new FileWriter(f);
// Write the text mentioned below to the file
fw.write("I\n am\n happy\n with\n my\n life\n ");
fw.flush();
fw.close();
// FileReader Object creation
FileReader frd = new FileReader(f);
// file reader's character encoding will be returned
System.out.println("The character encoding of the file f: " + frd.getEncoding());
// Closes the reader
frd.close();
}
}
Output:
In this program also, a file will be created in the specified location with the text mentioned. After that, an object of file reader will be created so that the data available in the text file can be read. Once it is done, the encoding of the text in the file is identified using the method getEncoding( ) and it gets printed.
Conclusion
FileReader in Java is a class that helps in reading characters or content from files. In this article, details such as declaration, constructors, methods, and examples of File Reader in Java are explained in a clear and concise manner.
Recommended Articles
This is a guide to FileReader in Java. Here we discuss the Examples of FileReader in Java along with the Constructor and Methods. You may also have a look at the following articles to learn more –