Files read files

1. Read as

1). Read as stream
generally read as streamprocessing 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 Stringconvert 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
Charsetor StandardCharsetscan be used to specify a specific code

Nearly 20,000 words explain the operation of JAVA NIO2 files in detail, enjoyable!

Guess you like

Origin blog.csdn.net/claroja/article/details/113872693