Java 8 文件操作(转)

Java 8 文件操作(转)

我们知道在JDK6甚至之前的时候,我们想要读取一个文本文件也是非常麻烦的一件事,而现在他们都变得简单了, 这要归功于NIO2,我们先看看之前的做法:

读取一个文本文件

BufferedReader br = null;
try {
    new BufferedReader(new FileReader("file.txt"));
    StringBuilder sb = new StringBuilder();
    String line      = br.readLine();
    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
} catch (Exception e){
    e.printStackTrace();
} finally {
    try {
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

大家对这样的一段代码一定不陌生,但这样太繁琐了,我只想读取一个文本文件,要写这么多代码还要 处理让人头大的一堆异常,怪不得别人吐槽Java臃肿,是在下输了。。。

下面我要介绍在JDK7中是如何改善这些问题的。

Path

Path用于来表示文件路径和文件,和File对象类似,Path对象并不一定要对应一个实际存在的文件, 它只是一个路径的抽象序列。

创建一个Path对象有多种方法,首先是final类Paths的两个static方法,如何从一个路径字符串来构造Path对象:

Path path1   = Paths.get("/home/biezhi", "a.txt");
Path path2   = Paths.get("/home/biezhi/a.txt");
URI  u       = URI.create("file:////home/biezhi/a.txt");
Path pathURI = Paths.get(u);

通过FileSystems构造

Path filePath = FileSystems.getDefault().getPath("/home/biezhi", "a.txt");

Path、URI、File之间的转换

File file  = new File("/home/biezhi/a.txt");
Path p1    = file.toPath();
p1.toFile();
file.toURI();

读写文件

你可以使用Files类快速实现文件操作,例如读取文件内容:

byte[] data    = Files.readAllBytes(Paths.get("/home/biezhi/a.txt"));
String content = new String(data, StandardCharsets.UTF_8);

如果希望按照行读取文件,可以调用

List<String> lines = Files.readAllLines(Paths.get("/home/biezhi/a.txt"));

反之你想将字符串写入到文件可以调用

Files.write(Paths.get("/home/biezhi/b.txt"), "Hello JDK7!".getBytes());

你也可以按照行写入文件,Files.write方法的参数中支持传递一个实现Iterable接口的类实例。 将内容追加到指定文件可以使用write方法的第三个参数OpenOption:

Files.write(Paths.get("/home/biezhi/b.txt"), "Hello JDK7!".getBytes(),
 StandardOpenOption.APPEND);

默认情况Files类中的所有方法都会使用UTF-8编码进行操作,当你不愿意这么干的时候可以传递Charset参数进去变更

当然Files还有一些其他的常用方法:

InputStream ins = Files.newInputStream(path);
OutputStream ops = Files.newOutputStream(path);
Reader reader = Files.newBufferedReader(path);
Writer writer = Files.newBufferedWriter(path);

创建、移动、删除

创建文件、目录

if (!Files.exists(path)) {
    Files.createFile(path);
    Files.createDirectory(path);
}

Files还提供了一些方法让我们创建临时文件/临时目录:

Files.createTempFile(dir, prefix, suffix);
Files.createTempFile(prefix, suffix);
Files.createTempDirectory(dir, prefix);
Files.createTempDirectory(prefix);

这里的dir是一个Path对象,并且字符串prefix和suffix都可能为null。 例如调用Files.createTempFile(null, “.txt”)会返回一个类似/tmp/21238719283331124678.txt

读取一个目录下的文件请使用Files.listFiles.walk方法

复制、移动一个文件内容到某个路径

Files.copy(in, path);
Files.move(path, path);

删除一个文件

Files.delete(path);