如何将文件从一个位置 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/