Read a text file using FileReader in Java
This post will discuss how to read the contents of a text file using a FileReader in Java.
We can use Reader such as FileReader for reading a text file in Java. FileReader is a convenience class meant for reading streams of characters from files that work only with the default character encoding.
To read the contents of a text file, the idea is to create a new FileReader and read the whole file character by character using Reader’s read() method, as shown below. It returns the character read, or -1 if the end of the stream is reached, and throws an IOException if an I/O error occurs.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.File; import java.io.FileReader; import java.io.IOException; class Main { public static void main(String[] args) { File file = new File("doc.txt"); try (FileReader fr = new FileReader(file)) { int content; while ((content = fr.read()) != -1) { System.out.print((char) content); } } catch (IOException e) { e.printStackTrace(); } } } |
The read() method has an overloaded version that can read a specified length of the characters from the input stream into an array of chars. We can use this method to read the whole file into a character array at once. The contents of the character array are then copied into a new string using the String constructor, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.File; import java.io.FileReader; import java.io.IOException; class Main { public static void main(String[] args) { File file = new File("doc.txt"); try (FileReader fr = new FileReader(file)) { char[] chars = new char[(int) file.length()]; fr.read(chars); String fileContent = new String(chars); System.out.println(fileContent); } catch (IOException e) { e.printStackTrace(); } } } |
In case the file is too large to read in a single read operation, we can use another overloaded version of the read() method, which can repeatedly read the specified number of chars into a buffer until the end of the file has been reached. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 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 37 38 |
import java.io.File; import java.io.FileReader; import java.io.IOException; class Main { public static String readFile(File file) throws IOException { try (FileReader fr = new FileReader(file)) { char[] chars = new char[(int)file.length()]; int offset = 0; while (offset < chars.length) { int result = fr.read(chars, offset, chars.length - offset); if (result == -1) { break; } offset += result; } return new String(chars); } } public static void main(String[] args) { File file = new File("doc.txt"); String content = null; try { content = readFile(file); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
Using FileReader is a very inefficient way of reading a text file in Java. To improve the performance in huge files, it is recommended to use a BufferedReader on top of a FileReader. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; class Main { public static void main(String[] args) { File file = new File("doc.txt"); try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } |
In Java 8 and above, we can use the BufferedReader.lines() method to get a stream of lines of text read from BufferedReader. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; class Main { public static void main(String[] args) { File file = new File("doc.txt"); try { BufferedReader br = new BufferedReader(new FileReader(file)); br.lines().forEach(System.out::println); // return br.lines().collect(Collectors.joining(System.lineSeparator())); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about reading a text file using FileReader in Java.
Read More:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)