gpt4 book ai didi

java - 在 Java 中如何将文件从一个位置 move 到另一个位置?

转载 作者:太空宇宙 更新时间:2023-11-04 10:45:15 28 4
gpt4 key购买 nike

如何将文件从一个位置 move 到另一个位置?当我运行程序时,在该位置创建的任何文件都会自动 move 到指定位置。我如何知道哪个文件被 move ?

最佳答案

myFile.renameTo(new File("/the/new/place/newName.file"));

File#renameTo这样做(它不仅可以重命名,还可以在目录之间 move ,至少在同一文件系统上)。

Renames the file denoted by this abstract pathname.

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

如果您需要更全面的解决方案(例如想要在磁盘之间 move 文件),请查看 Apache Commons FileUtils#moveFile

关于java - 在 Java 中如何将文件从一个位置 move 到另一个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48483150/

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