Java file FAQ: Can you demonstrate how to use the Java FileReader
class?
I have a number of examples on this site about how to read files with Java, but I've never dug into it from the aspect of how and why to use a Java FileReader
, so I thought I'd take a few moments and dig into the FileReader
class in this article.
The Java FileReader class
The FileReader javadoc provides a nice introduction to the FileReader
class:
The FileReader is a convenience class for reading character files ... FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.
The concept to take from those statements is “Use a FileReader
to read text files.”
You still need buffering(!)
That’s a good start, but you really don’t want to use a Java FileReader
by itself to read text files; you’re much better off to wrap a FileReader
with a BufferedReader. Unfortunately you won’t find that discussed in the FileReader
javadoc, but you will find it in the BufferedReader javadoc. Here are a couple of lines I just pulled from the BufferedReader javadoc:
A BufferedReader ... reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.
It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.
Besides this buffering — which makes your file-reading much faster — the BufferedReader
provides a convenience method named readLine
that makes it much easier to read text from input streams. Let’s take a look at this with some example Java file-reading code.
Reading a file with a BufferedReader and a FileReader
The following example code shows how I typically use the combination of a Java FileReader
and BufferedReader
to read a text file. I create the FileReader
with the name of the file I want to read, and then immediately wrap that FileReader
with a BufferedReader
:
private List<String> readFile(String filename) throws Exception { String line = null; List<String> records = new ArrayList<String>(); // wrap a BufferedReader around FileReader BufferedReader bufferedReader = new BufferedReader(new FileReader(filename)); // use the readLine method of the BufferedReader to read one line at a time. // the readLine method returns null when there is nothing else to read. while ((line = bufferedReader.readLine()) != null) { records.add(line); } // close the BufferedReader when we're done bufferedReader.close(); return records; }
As mentioned before, this gives me the dual benefits of (a) buffering the file-reading process, and (b) giving me access to BufferedReader
’s readLine
method, so I can handle read each line from the file as a String
.
For more information
Before I go, here are a few links with more information regarding the FileReader
and BufferedReader
classes, and my tutorial on how to read a file with Java, which includes a little more discussion than the code shown above:
- My Java BufferedReader tutorial
- My “How to open and read a file with Java” tutorial
- The FileReader javadoc
- The BufferedReader javadoc