1. Read as
1). Read as stream
generally read as stream
processing first , then convert to list
public static void main(String[] args) throws IOException {
Files.lines(Paths.get("./test")).forEach(System.out::println);
}
2). Read as list
Direct read aslist
public static void main(String[] args) throws IOException {
System.out.println(Files.readAllLines(Paths.get("./test")));
}
3) Read as a string
first read as bytes[]
, then use to new String
convert to a string
System.out.println(new String(Files.readAllBytes(Paths.get("./test"))));
2. Encoding settings
The default is the system code, the code can be set in the second parameter
Charset
or StandardCharsets
can be used to specify a specific code
Nearly 20,000 words explain the operation of JAVA NIO2 files in detail, enjoyable!