Read contents of a file in Java 8 and above
This post will discuss how to read the contents of a file in Java.
The java.nio.file.Files class in Java provides static methods that operate on files, directories, or other types of files. The Files class was first introduced with Java SE 7, and several new utility methods were added to it with Java 8. The following methods are included with Java 8 in the Files class, which are useful to read all content from a file.
1. Using Files.lines(Path) method
The Files.lines(Path) method can be used to read all lines from a file as a stream. It accepts the path to the source file and returns the lines from the file as a stream. The decoding from bytes to characters is done using the UTF-8 charset, i.e., this method is equivalent to: Files.lines(path, StandardCharsets.UTF_8). It throws an IOException if an I/O error occurs while opening the file.
Following is a simple example demonstrating the usage of this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; class Main { public static void main(String[] args) { Path path = Paths.get("demo.txt"); try (Stream<String> stream = Files.lines(path)) { stream.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
Note that the Files.lines() method doesn’t include line-termination characters. If we want to read all text from a file into a string in Java, we can do something like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Collectors; class Main { public static void main(String[] args) { String filePath = "doc.txt"; String content = null; try { content = Files.lines(Paths.get(filePath)) .collect(Collectors.joining(System.lineSeparator())); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
2. Using Files.lines(Path, Charset) method
Files.lines(Path, Charset) method can be used to read all lines from a file as a stream using the specified charset to decode bytes to characters.
It takes the path to the file and the charset used for decoding as parameters. It returns the lines from the file as a stream and throws an IOException if an I/O error occurs while opening the file. Following is a simple example demonstrating the usage of this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; class Main { public static void main(String[] args) { Path path = Paths.get("demo.txt"); try (Stream<String> stream = Files.lines(path, StandardCharsets.UTF_8)) { stream.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
3. Using BufferedReader.lines() method
Java 8 also introduced the BufferedReader.lines() method, which returns a stream of lines of text read from BufferedReader. This is demonstrated below using Stream:
|
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; import java.util.stream.Collectors; class Main { public static void main(String[] args) { String filePath = "doc.txt"; try (BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)))) { reader.lines().forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
Similar to the Files.lines() method, the BufferedReader.lines() method doesn’t include line-termination characters. If we want to read all text from a file into a string in Java, we can use Java 8 Stream collect() method, as demonstrated earlier.
That’s all about reading the contents of a file 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 :)