Contents
Class FileReader in Java
FileReader is a convenience subclass of InputStreamReader that is useful when we want to read a text (as opposed to binary data) from a file. We create a FileReader by specifying the file to be read, in any of three possible forms.
The FileReader constructor internally creates a FileInputStream to read bytes from the specified file, and uses the functionality of its superclass, InputStreamReader, to convert those bytes from characters in the local encoding to the Unicode characters used by Java.
The FileReader class represents a character stream that reads data from a file. It is a subclass of InputStreamReader that uses a default buffer size (8192 bytes) to read bytes from a file and the default character encoding scheme to convert the bytes to characters. If you need to specify the character encoding or the buffer size, wrap an InputStreamReader around a FileInputStream.
The file can be specified using a FileDescriptor, a File object, or a String that represents a
pathname. All of the constructors can throw a SecurityException if the application does not have permission to read from the specified file.
FileReader provides a low-level interface for reading character data from a file. we can think
about wrapping a FileReader with a BufferedReader to increase reading efficiency.
If we need to read binary data from a file, we can use a FileInputStream wrapped by a
DataInputStream instead.
Because FileReader is a trivial subclass of InputStreamReader, it does not define any read() methods or other methods of its own. Instead, it inherits all its methods from its superclass.
If we want to read Unicode characters from a file that uses some encoding other than the default encoding for the locale, we must explicitly create our own InputStreamReader to perform the byte-to-character conversion.
The structure of the class FileReader is given by
public class java.io.FileReader extends java.io.InputStreamReader{
// Public Constructors
public FileReader(String fileName) throws FileNotFoundException;
public FileReader(File file) throws FileNotFoundException;
public FileReader(FileDescriptor fd);
}
The details of the class structure are given as follows:
public FileReader(String fileName) throws FileNotFoundException;
public FileReader(String fileName) constructor creates a FileReader that gets its input from the file named by the specified String.
Parameter
fileName -A String that contains the pathname of the file to be accessed. The path must conform to the requirements of the native operating system.
public FileReader(File file) throws FileNotFoundException;
public FileReader(File file) throws FileNotFoundException constructor creates a FileReader that gets its input from the file represented by the specified File.
Parameter
file – The File to use as input.
public FileReader(FileDescriptor fd);
public FileReader(FileDescriptor fd) constructor creates a FileReader that gets its input from the file identified by the given FileDescriptor.
Parameter
fd– The FileDescriptor of the file to use as input.
Apart from these FileReaderclass also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)
FileReaderclass also has inherited methods from InputStreamReader. They are as follows:
- ready()
- close()
- getEncoding()
- read()
- read(char[], int,int)
FileReaderclass also has inherited one methods from Reader. That is as follows:
- read(char[])