FileReader Java Example
In this example, we are going to see how to use FileReader Java class in order to read characters from a file. As you might remember from a previous FileInpustream Example it is easy to wrap an InputStream
, that creates an input byte stream, to a Reader
class that bridges a byte stream to a character stream. To be more convenient, Java offers FileReader
that directly connects a file to an input character stream, so you can directly read characters from it.
Let’s see some FileReader Java examples in the next chapters.
1. Introduction
The first thing that we must know is what FileReader does. The FileReader class of the java.io package can be used to read data (in characters) from files. The FileReader extends the InputStreamReader class which extends Reader.
2. Technologies Used
The example code in this article was built and run using:
- Java 1.8.231(1.8.x will do fine)
- Eclipse IDE for Enterprise Java Developers-Photon
3. FileReader constructors
There three different FileReader constructors:
- FileReader(File f): Creates a new FileReader given the File to read from.
- FileReader(String f): Creates a new FileReader, given the name of the file to read from.
- FileReader(FileDescriptor f): Creates a new FileReader, given the FileDescriptor to read from.
4. FileReader methods in Java
There are two FileReader methods which are:
- public int read(): This method reads a single character and returns an int, which represents the character read.
- public int read(char[] a, int b, int c): This method reads characters int an array and returns the number of the characters read.
5. FileReader alternatives
Some alternative ways to read text files in Java are:
- Files.readAllLines(): This method reads all lines from a file and ensures that the file is closed when all bytes have been read or an exception is thrown. The bytes from the file are decoded into characters using the specified charset.
- Reading text file with Java 8 streaming API: The Files.lines() reads all lines from a file as a stream. The bytes from the file are decoded into characters using the StandardCharsets.UTF-8 charset.
- InputStreamReader: The InputStreamsReader reads bytes and decodes them into characters using a specified charset.
- FileChannel: The FileChannel is a channel for reading, writing, mapping, and manipulating a file.
- Scanner: This way can parse primitive types and strings using regular expressions.
6. Simple FileReader Java Examples
Let’s see the first example:
FileReaderExample.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.javacodegeeks.core.io.outputstream; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; public class FileReaderExample { private static final String OUTPUT_FILE = "C:\\Users\\nikos\\Desktop\\TestFiles\\testFile.txt" ; public static void main(String[] args) { char [] chars = new char [ 100 ]; char [] chars2 = new char [ 100 ]; try (FileReader fileReader = new FileReader( new File(OUTPUT_FILE))) { // read a single bytes int r = fileReader.read(); System.out.println( "Read byte :" +r); // read a sequence of bytes and store it to a char[] array int charsRead = fileReader.read(chars); System.out.println( "Read bytes : " +charsRead); System.out.println(Arrays.toString(chars)); // read a sequence of bytes and store it to arbitrary position to a char[] array charsRead = fileReader.read(chars2, 5 , 30 ); System.out.println( "Read bytes : " +charsRead); System.out.println(Arrays.toString(chars2)); } catch (IOException e) { e.printStackTrace(); } } } |
This is the output:
Read byte :105 Read bytes : 100 [a, s, i, d, a, i, o, s, d, h, i, o, h, a, d, h, a, i, s, h, f, a, i, s, h, f, o, i, a, h, f, o, i, a, s, f, a, i, s, h, f, o, i, h, a, o, s, f, i, h, o, a, s, f, a, s, i, f, h, o, a, h, s, f, o, i, h, a, s, i, o, f, h, a, i, s, h, f, o, a, i, s, h, f, i, o, a, h, f, i, o, a, s, h, f, o, i, a, s, h] Read bytes : 30 [, , , , , f, o, i, a, h, s, o, i, f, i, a, j, s, f, j, a, i, o, j, f, o, a, j, s, f, o, i, a, j, s, , , , , , , , , , , , , , , , , , , ]
As you can see, it’s very easy to read characters from the file and store it into a char[]
array. You can either choose to read a single character, or a sequence of characters, which you can either use to fill up a char array or store it in an arbitrary offset in the char array.
7. Buffering a FileReader Java Example
Of course you can also buffer an FileReader
using an BufferedReader
. If your application is very I/O intensive and it intends to read large amounts of data from large files, then it’s highly advised to buffer the FileReader
. For that, you can use a BufferedReader
. This will automatically create an internal buffer and perform as less I/O operations as possible. You can also choose the internal buffer size.
BufferedReader
offers a very convenient readLine
method that enables to read character streams line by line. I
Let’s see how you can read a text file line by line:
FileReaderExample.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.javacodegeeks.core.io.outputstream; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileReaderExample { private static final String OUTPUT_FILE = "C:\\Users\\nikos\\Desktop\\TestFiles\\testFile.txt" ; public static void main(String[] args) { String str = "" ; try (BufferedReader bufReader = new BufferedReader( new FileReader( new File(OUTPUT_FILE)), 1024 )) { while ( ( str = bufReader.readLine() ) != null ) System.out.println(str); } catch (IOException e) { e.printStackTrace(); } } } |
This is the output:
aoifjaiofjaoisfjaoisjaosijfaoisjfoiasjfioasjinoasjcniajna]aifja]fa]sfafa asfajsifjaoisjfoiiasidaiosdhiohadhaishfaishfoiahfoiasfaishfoihaosfihoasf asifhoahsfoihasiofhaishfoaishfioahfioashfoiashfoiahsoif iajsfjaiojfoajsfoiajsoifajsoifjasfjsajfoasjfoiasjfoisajfoia oiasjfojaoifjaiofjaoisfjaoisjaosijfaoisjfoiasjfioasjinoasjcniajna]aifja]fa]sfafa iasidaiosdhiohadhaishfaishfoiahfoiasfaishfoihaosfihoasf asifhoahsfoihasiofhaishfoaishfioahfioashfoiashfoiahsoif iajsfjaiojfoajsfoiajsoifajsoifjasfjsajfoasjfoiasjfoisajfoia oiasjfojaoifjaiofjaoisfjaoisjaosijfaoisjfoiasjfioasjinoasjcniajna]aifja]fa]sfafa asfajsifjaoisjfoiiasidaiosdhiohadhaishfaishfoiahfoiasfaishfoihaosfihoasf ...
8. Download the source code
This was a FileReader Java Example.
You can download the source code of this example here: FileReader Java Example
Last updated on Mar. 16th, 2020