Java文件I/O(NIO.2)与并发编程
1. Java文件I/O(NIO.2)
1.1 使用 Files 类的 lines() 方法
Files.lines() 是一个非常方便的读取文件内容的方法,其定义如下:
static Stream<String> lines(Path path)
该方法内部使用了 Reader ,因此使用后必须关闭。以下是一个使用 try-with-resources 语句打印文件内容的示例代码:
import java.io.IOException;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.stream.Stream;
// implements a simplified version of "type" command provided in Windows;
// given a text file name(s) as argument, it prints the content of the file(s)
class Type {
private static void processFile(String file) {
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



