Files 读文件

1.读取为

1).读取为stream
一般都是先读取为stream处理后再,转成list

    public static void main(String[] args) throws IOException {
    
    
        Files.lines(Paths.get("./test")).forEach(System.out::println);
    }

2).读取为list
直接读取为list

public static void main(String[] args) throws IOException {
    
    
    System.out.println(Files.readAllLines(Paths.get("./test")));
}

3)读取为字符串
先读取为bytes[],然后使用new String转换为字符串

System.out.println(new String(Files.readAllBytes(Paths.get("./test"))));

2.编码设置

默认为系统编码,可以在第二个参数设置编码
Charset或者StandardCharsets可以用来指定具体的编码

近2万字详解JAVA NIO2文件操做,过瘾!

猜你喜欢

转载自blog.csdn.net/claroja/article/details/113872693