JDK1. 7 Java nio. file. Files only needs one line of code to read files
JDK1. 7 introduced a new file operation class java nio. The file package contains a files class, which contains many useful methods to manipulate files, such as checking whether the file is hidden or read-only. Developers can also use files The readallbytes (path) method reads the entire file into memory. This method returns a byte array. It can also pass the result to the constructor of string to create string output. This method ensures that when all bytes of the file are read in, the resource will be shut down regardless of whether there are IO exceptions or other unchecked exceptions. This means that there is no need to close the file after reading the last block of the file. Note that this method is not suitable for reading large files because there may be insufficient memory space. Developers should also specify the character encoding of the file to avoid any exceptions or parsing errors.
Source code of readallbytes (path) method:
Only one line is needed to read the file
Readalllines method source code
How to use java8 streams:
First look at the source code implementation
One line is required to read a file and only one line is required to write a file
The above jdk1 7 Java nio. file. Files only needs one line of code to read the file, which is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.