使用jdk7,我试图将java.nio.file.Files
类用于move一个空目录,比方说 Bar
,到另一个空目录,比方说 Foo
Path source = Paths.get("Bar");
Path target = Paths.get("Foo");
try {
Files.move(
source,
target,
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
执行该代码片段后,我预计 Bar
目录将位于 Foo
目录中 (...\Foo\Bar
).相反,它不是。这是踢球者,它也被删除了。此外,没有抛出异常。
我做错了吗?
注意
我正在寻找一个特定于 jdk7 的解决方案。我也在调查这个问题,但我想我会看看是否还有其他人在玩 jdk7。
编辑
除了已接受的答案外,这里还有 another solution
Path source = Paths.get("Bar");
Path target = Paths.get("Foo");
try {
Files.move(
source,
target.resolve(source.getFileName()),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
最佳答案
我没有意识到 jdk7 java.nio.file.Files 是必需的,所以这是编辑后的解决方案。请看看它是否有效,因为我以前从未使用过新的 Files 类。
Path source = Paths.get("Bar");
Path target = Paths.get("Foo", "Bar");
try {
Files.move(
source,
target,
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
关于java - 如何使用jdk7移动目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6210433/