如何使用jdk7移动目录 [英] How to move directories using jdk7
问题描述
使用 jdk7 ,我正在尝试使用java.nio.file.Files
类来
Using jdk7, I am trying to use the java.nio.file.Files
class to move an empty directory, let's say Bar
, into another empty directory, let's say 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
)中.相反,事实并非如此.这是踢脚线,它也已被删除.另外,没有引发异常.
After executing that code snippet, I expected that the Bar
directory would be in the Foo
directory (...\Foo\Bar
). Instead it is not. And here's the kicker, it's been deleted as well. Also, no exceptions were thrown.
我做错了吗?
注意
我正在寻找特定于 jdk7的解决方案.我也在调查问题,但我想知道是否还有其他人在玩jdk7.
I'm looking for a jdk7-specific solution.I am also looking into the problem, but I figured I'd see if there was anyone else playing around with jdk7.
编辑
In addition to the accepted answer, here's 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类.
I didn't realize jdk7 java.nio.file.Files is a necessity, so here is the edited solution. Please see if it works coz I have never used the new Files class before.
Path source = Paths.get("Bar");
Path target = Paths.get("Foo", "Bar");
try {
Files.move(
source,
target,
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
这篇关于如何使用jdk7移动目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!