Java FileReader tutorial
last modified July 6, 2020
Java FileReader tutorial shows how to use FileReader
class to read
text files in Java.
Java tutorial is a comprehensive tutorial on Java language.
The examples use this text file:
The Battle of Thermopylae was fought between an alliance of Greek city-states, led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the course of three days, during the second Persian invasion of Greece.
Java FileReader
FileReader
is a Java convenience class for reading text data to
files. FileReader
extends InputStreamReader
and creates
the FileInputStream
.
FileReader
relied on the
default platform's encoding. Since Java 11, the issue was corrected. It is
possible now to explicitly specify the encoding. Always specify the encoding
when using FileReader
.
Java FileReader example
In the following example, we use FileReader
to
read a text file.
package com.zetcode; import java.io.FileReader; import java.io.IOException; import java.nio.charset.StandardCharsets; public class FileReaderEx { public static void main(String[] args) throws IOException { var fileName = "src/resources/thermopylae.txt"; char[] buf = new char[1024]; try (var fr = new FileReader(fileName, StandardCharsets.UTF_8)) { int c; while ((c = fr.read(buf)) != -1) { System.out.println(buf); } } } }
The example reads a text file. It uses a character buffer to improve the performance of the operation.
try (var fr = new FileReader(fileName, StandardCharsets.UTF_8)) {
The first argument of the FileReader
is the file name.
The second is the encoding type. We use try-with-resources construct to clean
resources after we have finished writing.
int c; while ((c = fr.read(buf)) != -1) { System.out.println(buf); }
The FileReader's
read()
method reads characters into
an array. It returns the the number of characters read, or -1 if the end of the
stream has been reached.
Java FileReader with BufferedReader
FileReader's
performance can be improved with
BufferedReader
. BufferedReader
reads text from a
character-input stream, buffering characters so as to provide for the efficient
reading of characters, arrays, and lines. The buffer size may be specified, or
the default size may be accepted; the default is large enough for most purposes.
package com.zetcode; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.nio.charset.StandardCharsets; public class JavaFileReaderBuffered { public static void main(String[] args) throws IOException { var fileName = "src/resources/thermopylae.txt"; try (var br = new BufferedReader(new FileReader(fileName, StandardCharsets.UTF_8))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } } }
In the example, we wrap the FileReader
into the BufferedReader
to improve its performance.
String line; while ((line = br.readLine()) != null) { System.out.println(line); }
The readLine()
reads a line of text. It returns the string
containing the contents of the line, not including any line-termination
characters, or null
if the end of the stream has been reached
without reading any characters.
In this tutorial, we have presented the Java FileReader
class.
Read Java tutorial or list all Java tutorials.
List all Java tutorials.