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

标签 java file move

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

相关文章:

android - 如何使用动画 move 和淡出任何 View

java - PathMatchingResourcePatternResolver 返回的资源可以是 "directory"。如何避免

java - 如何使用 itext 打开受密码保护的 pdf

Python3 : Files organizing

windows - 在批处理文件中复制( move )文件的快速方法

c++ - 通过 `&&` 传递的参数对非构造函数有用吗?

java - Servlet 链接相对于应用程序基础

java - 如何定义REST服务契约以便在多个相似服务之间共享使用?

c - 在 C 中分割文件中的二进制文件而不会损坏

javascript - 如何创建一个弹出窗口来选择本地目录中的文件?