如何在Java 7 java.nio.file.Path中访问子文件/文件夹?


67

Java 7引入了java.nio.file.Path作为java.io.File的可能替代

使用文件,当我访问特定目录下的文件时,我将执行以下操作:

File parent = new File("c:\\tmp");
File child = new File(parent, "child"); // this accesses c:\tmp\child

用Path执行此操作的方式是什么?

我以为这会工作:

Path parent = Paths.get("c:\\tmp");
Path child = Paths.get(parent.toString(), "child");

但是打电话parent.toString()似乎很丑。有没有更好的办法?

Answers:


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.