【发布时间】:2020-06-16 15:34:19
【问题描述】:
我目前正在掌握 Java 中的文件管理。据我所知,java.nio.file.Path 是这样做的首选方式。
假设我想将 oldDir 的内容复制到当前为空的 newDir。每次复制文件时,我都需要这条长线来获取 newFile 的路径:
Path newDir = FileSystems.getDefault().getPath("new");
Path oldDir = FileSystems.getDefault().getPath("old");
for (Path oldFile : oldDir) {
Path newFile = FileSystems.getDefault().getPath("new", oldFile.getFileName().toString()); // Why so complicated? :(
Files.copy(oldFile, newFile);
}
有没有像newDir.getChild(oldFile.getFileName()) 这样的东西来做我想做的事,或者真的没有更短的方法吗?
【问题讨论】:
-
newDir.resolve(oldFile.getFileName())
标签: java file io java.nio.file