Read a file using BufferedReader in Java
This post will discuss how to read the contents of a file using BufferedReader in Java.
The BufferedReader class in Java reads text from a character-input stream, buffering characters so as to provide for the efficient read. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. Therefore, it is advisable to wrap a BufferedReader around any Reader whose read operations may be costly, such as FileReaders and InputStreamReaders.
1. BufferedReader’s readLine() method
BufferedReader’s readLine() method reads a line of text. Each invocation of the readLine() method would read bytes from the file, convert them into characters, and then return. This is demonstrated below using a FileReader for reading a text file:
|
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("demo.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(); } } } |
FileReader is meant for reading streams of characters. Another solution is to use the BufferedReader with InputStreamReader.
The following code reads streams of raw bytes using FileInputStream and decodes them into characters using a specified charset using an InputStreamReader, and form a string using a platform-dependent line separator.
|
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 |
import java.io.*; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; class Main { public static String readFile(File file, Charset encoding) throws IOException { StringBuilder sb = new StringBuilder(); FileInputStream fileStream = new FileInputStream(file); BufferedReader br; String line; br = new BufferedReader(new InputStreamReader(fileStream, encoding)); while ((line = br.readLine()) != null) { sb.append(line + System.lineSeparator()); } return sb.toString(); } public static void main(String[] args) { File file = new File("demo.txt"); String content = null; try { content = readFile(file, StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
2. BufferedReader’s lines() method
In Java 8 and above, we can use the BufferedReader’s 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 |
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("demo.txt"); try { BufferedReader reader = new BufferedReader(new FileReader(file)); reader.lines().forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about reading a file using BufferedReader 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 :)