gpt4 book ai didi

java.nio.file Files.move() 是复制而不是移动

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:25 28 4
gpt4 key购买 nike

我正在尝试使用 Java NIO Files.move 方法移动目录。它会将目录内容复制到新位置,但会保留旧目录。我认为这是复制操作而不是移动操作。

知道为什么会这样吗?这是我的代码:

Path source = FileSystems.getDefault().getPath("C:\\test-source");
Path destination = FileSystems.getDefault().getPath("C:\\test-destination");
try {
System.out.println("Moving files ...");
Files.move(source, destination, StandardCopyOption.ATOMIC_MOVE);
System.out.println("Done.");
} catch (IOException e) {
System.out.println("Moving failed: " + e.toString());
}

同样,目标目录及其所有内容都会出现,但源文件夹仍保留在原处。

最佳答案

来自 this

ATOMIC_MOVE 是一个文件操作。

public static final StandardCopyOption ATOMIC_MOVE

将文件作为原子文件系统操作移动。

试试 StandardCopyOption.REPLACE_EXISTING

关于java.nio.file Files.move() 是复制而不是移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34733765/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com