Table of Contents
Java FileReader
- Java FileReader class is part of java.io package.
- The FileReader is a subclass of
InputStreamReader
class. - Java FileReader is recommended for reading text data from a file compared to
FileInputStream
. - FileReader is meant for reading streams of characters. So it’s a good choice to read String based data.
- The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate.
- FileReaders are usually wrapped by higher-level objects such as
BufferedReader
, which improve performance and provide more convenient ways to work with the data.
FileReader Class Hierarchy
FileReader Constructors
Let’s have a quick look at FileReader constructors.
FileReader(File file)
: Creates a new FileReader object using specified file object to read from. It will throwFileNotFoundException
if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.FileReader(FileDescriptor fd)
: Creates a new FileReader object using specifiedFileDescriptor
object to read from.FileReader(String fileName)
: Creates a new FileReader object using specified name of the file to read from. It will throwFileNotFoundException
if the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
Java FileReader Example
Let’s have a look at the below methods and example programs of FileReader class.
read()
This method reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached. It returns the character read as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached.
Let’s have look at the below example program.
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
/**
* Java Read file FileReader
*
* @author pankaj
*
*/
public class FileReaderReadExample {
public static void main(String[] args) {
File file = null;
FileReader reader = null;
try {
file = new File("D:/data/file.txt");
reader = new FileReader(file);
int i;
while ((i=reader.read())!= -1) {
System.out.print((char)i);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
The output of the above program is below:
Hello World.
This is a FileReader Example.
FileReader implements AutoCloseable
interface, hence we can user try with resource while using FileReader class. Let’s have look at the below example program.
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
/**
* Java Read file FileReader using try with resource
*
* @author pankaj
*
*/
public class FileReaderReadUsingTryWithResource {
public static void main(String[] args) {
File file = new File("D:/data/file.txt");
try (FileReader reader = new FileReader(file);){
int i;
while ((i=reader.read())!= -1) {
System.out.print((char)i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output of the above program is below:
Hello World.
This is a FileReader Example.
read(char[] cbuf)
This method reads characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. It returns the number of characters read, or -1 if the end of the stream has been reached. Let’s have look at the below example program.
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
/**
* Java Read file FileReader using read(char[] cbuf) method
*
* @author pankaj
*
*/
public class ReadFileUsingFileReader {
public static void main(String[] args) {
File file = new File("D:/data/file.txt");
try (FileReader reader = new FileReader(file);){
char[] cs = new char[100];
reader.read(cs);
for (char c : cs) {
System.out.print(c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
The output of the above program is below:
Hello World.
This is a FileReader Example.
read(char[] cbuf, int off, int len)
This method reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. It returns the number of characters read, or -1 if the end of the stream has been reached.
Parameters:
- cbuf : Destination buffer
- off : Offset at which to start storing characters
- len : Maximum number of characters to read
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
/**
* Java Read file FileReader using read(char[] cbuf, int off, int len) method
*
* @author pankaj
*
*/
public class ReadFileUsingFileReaderExample {
public static void main(String[] args) {
File file = new File("D:/data/file.txt");
try (FileReader reader = new FileReader(file);){
char[] cs = new char[100];
reader.read(cs, 0, 11);
for (char c : cs) {
System.out.print(c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//Output: Hello World
read(CharBuffer target)
This method reads characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed. It returns the number of characters added to the buffer, or -1 if this source of characters is at its end.
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
import java.nio.CharBuffer;
/**
* Java Read file FileReader using CharBuffer
*
* @author pankaj
*
*/
public class ReadFileUsingFileReaderCharBuffer {
public static void main(String[] args) {
File file = new File("D:/data/file.txt");
try (FileReader reader = new FileReader(file);){
//create char buffer with the capacity of 50
CharBuffer cs = CharBuffer.allocate(50);
//read characters into char buffer
reader.read(cs);
//flip char buffer
cs.flip();
System.out.println(cs.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
skip(long n)
This method skips the n number of character and returns the number of skipped characters.
package com.journaldev.examples;
import java.io.File;
import java.io.FileReader;
/**
* Java Read file FileReader using skip method
*
* @author pankaj
*
*/
public class FileReaderSkipExample {
public static void main(String[] args) {
File file = null;
FileReader reader = null;
try {
file = new File("D:/data/file.txt");
reader = new FileReader(file);
//skip first 6 characters
reader.skip(6);
int i;
while ((i=reader.read())!= -1) {
System.out.print((char)i);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
Output:
World.
This is a FileReader Example.
Also check java read text file for more about how to read text file in java.
Java FileReader vs FileInputStream
- FileReader is used for reading streams of character while FileInputStream is used for reading streams of bytes like raw image data.
- FileReader is good for reading text file like java source code file while FileInputStream is good for reading binary files like .class files.
That’s all for Java FileReader, I hope nothing important got missed here.
Reference: API Doc