Java FileReader Tutorial with Examples
1. FileReader
No ADS
FileReader is a subclass of InputStreamReader, which is used to read text files.
FileReader has no more methods than methods inherited from InputStreamReader, you can actually use InputStreamReader to read characters from any source. However, FileReader is specifically designed to read characters from file system.
FileReader constructors
FileReader(File file)
FileReader(FileDescriptor fd)
FileReader(File file, Charset charset)
FileReader(String fileName)
FileReader(String fileName, Charset charset)
Note: Constructors with Charset parameter were added to FileReader since Java 11. So if you are using older Java and want to read a file with specified encoding, let use InputStreamReader class instead.
2. Examples
No ADS
For example, read a text file:
file-test.txt
File Content
FileReaderEx1.java
package org.o7planning.filereader.ex;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
public class FileReaderEx1 {
public static void main(String[] args) throws MalformedURLException, IOException {
File file = new File("file-test.txt");
FileReader fis = new FileReader(file);
int charCode;
while((charCode = fis.read()) != -1) {
System.out.println((char)charCode + " " + charCode);
}
fis.close();
}
}
Output:
F 70
i 105
l 108
e 101
32
C 67
o 111
n 110
t 116
e 101
n 110
t 116
When reading a text file, you should use a combination of BufferedReader and FileReader for best performance:
students.txt
# Students:
John P
Sarah M
# Sarah B
Charles B
Mary T
Sophia B
FileReaderEx2.java
package org.o7planning.filereader.ex;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.MalformedURLException;
public class FileReaderEx2 {
public static void main(String[] args) throws MalformedURLException, IOException {
File file = new File("students.txt");
Reader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
Output:
# Students:
John P
Sarah M
# Sarah B
Charles B
Mary T
Sophia B
Example: Read a text file and print out text lines does not start with character '#' (comment line):
FileReaderEx3.java
package org.o7planning.filereader.ex;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.MalformedURLException;
public class FileReaderEx3 {
public static void main(String[] args) throws MalformedURLException, IOException {
File file = new File("students.txt");
Reader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
br.lines() // java.util.stream.Stream
.filter(line -> !line.startsWith("#")) // Not starts with "#".
.forEach(System.out::println);
br.close();
}
}
Output:
John P
Sarah M
Charles B
Mary T
Sophia B
3. UTF-8 BOM Problem!
No ADS
Before UTF-8 became popular, UTF-8 file creators always added the first 3 bytes to mark that this file was UTF-8 encoded, they are called BOM (Byte Order Mark). While UTF-8 files created by Java do not include BOM.
FileReader does not automatically remove BOM when reading UTF-8 files. Java design team understood this. However, no update was made as it would break previous Java-based third-party libraries such as XML Parser,...
Example, below is a UTF-8 (BOM) file created by an old tool, you can download it to test problem being discussed:
utf8-file-with-bom-test.txt
Hello
Use FileReader to read above file:
FileReader_Utf8_BOM.java
package org.o7planning.filereader.ex;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
public class FileReader_Utf8_BOM {
public static void main(String[] args) throws MalformedURLException, IOException {
File file = new File("utf8-file-with-bom-test.txt");
System.out.println("--- Read by FileReader ---");
readByFileReader(file);
System.out.println("--- Read by InputStreamReader ---");
readByInputStreamReader(file);
}
private static void readByFileReader(File file) throws IOException {
FileReader fr = new FileReader(file, StandardCharsets.UTF_8);
int charCode;
while ((charCode = fr.read()) != -1) {
System.out.println((char) charCode + " " + charCode);
}
fr.close();
}
private static void readByInputStreamReader(File file) throws IOException {
InputStream is = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
int charCode;
while ((charCode = isr.read()) != -1) {
System.out.println((char) charCode + " " + charCode);
}
isr.close();
}
}
Output:
--- Read by FileReader ---
65279
H 72
e 101
l 108
l 108
o 111
--- Read by InputStreamReader ---
65279
H 72
e 101
l 108
l 108
o 111
Character with code 65279 appears in the result, which is an unwanted character.
Some of the following classes support BOM elimination you might consider using:
BOMInputStream
BOMInputStream is a class in Apache Commons IO library that supports BOM removal.
Maven dependency
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
BOMInputStreamEx1.java
package org.o7planning.filereader.ex;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.input.BOMInputStream;
public class BOMInputStreamEx1 {
public static void main(String[] args) throws IOException {
File file = new File("utf8-file-with-bom-test.txt");
FileInputStream fis = new FileInputStream(file);
BOMInputStream bis = new BOMInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis, StandardCharsets.UTF_8);
int charCode;
while ((charCode = isr.read()) != -1) {
System.out.println((char) charCode + " " + charCode);
}
isr.close();
}
}
Output:
H 72
e 101
l 108
l 108
o 111
UnicodeReader
UnicodeReader is a class in the "Google Data Java Client Library" that supports BOM removal.
Maven dependency
<!-- https://mvnrepository.com/artifact/com.google.gdata/core -->
<dependency>
<groupId>com.google.gdata</groupId>
<artifactId>core</artifactId>
<version>1.47.1</version>
</dependency>
UnicodeReaderEx1.java
package org.o7planning.filereader.ex;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import com.google.gdata.util.io.base.UnicodeReader;
public class UnicodeReaderEx1 {
public static void main(String[] args) throws IOException {
File file = new File("utf8-file-with-bom-test.txt");
FileInputStream fis = new FileInputStream(file);
UnicodeReader isr = new UnicodeReader(fis, "UTF-8");
int charCode;
while ((charCode = isr.read()) != -1) {
System.out.println((char) charCode + " " + charCode);
}
isr.close();
}
}
Output:
H 72
e 101
l 108
l 108
o 111
No ADS
Java IO Tutorials
- Java CharArrayWriter Tutorial with Examples
- Java FilterReader Tutorial with Examples
- Java FilterWriter Tutorial with Examples
- Java PrintStream Tutorial with Examples
- Java BufferedReader Tutorial with Examples
- Java BufferedWriter Tutorial with Examples
- Java StringReader Tutorial with Examples
- Java StringWriter Tutorial with Examples
- Java PipedReader Tutorial with Examples
- Java LineNumberReader Tutorial with Examples
- Java PrintWriter Tutorial with Examples
- Java IO Binary Streams Tutorial with Examples
- Java IO Character Streams Tutorial with Examples
- Java BufferedOutputStream Tutorial with Examples
- Java ByteArrayOutputStream Tutorial with Examples
- Java DataOutputStream Tutorial with Examples
- Java PipedInputStream Tutorial with Examples
- Java OutputStream Tutorial with Examples
- Java ObjectOutputStream Tutorial with Examples
- Java PushbackInputStream Tutorial with Examples
- Java SequenceInputStream Tutorial with Examples
- Java BufferedInputStream Tutorial with Examples
- Java Reader Tutorial with Examples
- Java Writer Tutorial with Examples
- Java FileReader Tutorial with Examples
- Java FileWriter Tutorial with Examples
- Java CharArrayReader Tutorial with Examples
- Java ByteArrayInputStream Tutorial with Examples
- Java DataInputStream Tutorial with Examples
- Java ObjectInputStream Tutorial with Examples
- Java InputStreamReader Tutorial with Examples
- Java OutputStreamWriter Tutorial with Examples
- Java InputStream Tutorial with Examples
- Java FileInputStream Tutorial with Examples
Show More