Read contents of a file in Java 11 and above
This post will discuss how to read the contents of a file in Java 11.
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 each subsequent release. Java 11 included the following methods in the Files class, which are useful to read all content from a file.
1. Using Files.readString(Path) method
Files.readString(Path) method can be used to read all characters from a file into a string. It accepts the path to the source file and returns a string containing the content read from the file. The decoding from bytes to characters is done using the UTF-8 charset, i.e., this method is equivalent to readString(path, StandardCharsets.UTF_8). It throws an IOException if any I/O error occurs during reading from 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 22 23 24 25 26 |
import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; class Main { public static String readFile(String path) throws IOException { return Files.readString(Paths.get(path)); } public static void main(String[] args) { String filePath = "doc.txt"; String content = null; try { content = readFile(filePath); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
2. Using Files.readString(Path, Charset) method
Files.readString(Path, Charset) method is used to read all characters from a file into a string 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 a string containing the content read from the file and throws an IOException if any I/O error occurs during reading from 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 22 23 24 25 26 |
import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; class Main { public static String readFile(String path, Charset encoding) throws IOException { return Files.readString(Paths.get(path), encoding); } public static void main(String[] args) { String filePath = "doc.txt"; String content = null; try { content = readFile(filePath, StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } System.out.println(content); } } |
That’s all about reading the contents of a file in Java 11 and above.
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 :)