Read contents of a file using Files class in Java
This post will discuss how to read the contents of a file using the Files class in Java 7.
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. 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.readAllLines(Path, Charset) method
To read all lines from a file, we can use the Files.readAllLines() method. It takes the path to the file and the charset to use for decoding from bytes to characters. It returns the lines from the file as a list and throws an IOException if an I/O error occurs reading from the stream.
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 22 |
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.List; class Main { public static void main(String[] args) { Path path = Paths.get("demo.txt"); try { List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); lines.stream().forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
With Java 8, Files.readAllLines(Path, Charset) method is overloaded to accept a single parameter, which is the file’s path. The remaining usage remains the same, i.e., it returns a list that can be easily iterated.
Note that this method is convenient to read all lines in a single operation but strips line terminators from the end of each line. We can easily handle this by joining each of the elements of the list with a line separator, 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 24 |
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.List; class Main { public static void main(String[] args) { Path path = Paths.get("demo.txt"); String content = null; try { List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); content = String.join(System.lineSeparator(), lines); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
2. Using Files.readAllBytes(Path) method
We can also use the readAllBytes() method, which takes the path to the file and returns a byte array containing the bytes read from the file. To get output in the string format, pass the byte array to the String constructor, as demonstrated below. The method throws an IOException if an I/O error occurs reading from the stream.
|
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.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; class Main { public static void main(String[] args) { Path path = Paths.get("demo.txt"); String content = null; try { byte[] encoded = Files.readAllBytes(path); content = new String(encoded); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
To get output in the string format, pass the byte array to the String constructor with a charset for decoding.
|
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.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; class Main { public static void main(String[] args) { Path path = Paths.get("demo.txt"); String content = null; try { byte[] encoded = Files.readAllBytes(path); content = new String(encoded, StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
That’s all about reading the contents of a file using the Files class 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 :)