Java:将包含文件和目录的目录移动到新路径
我有一个包含一些文件的目录,子目录中有更多的文件Java:将包含文件和目录的目录移动到新路径,java,file,unix,Java,File,Unix,我有一个包含一些文件的目录,子目录中有更多的文件 Folder - Directory (path -> /home/abc/xyz/Folder) ->Abc.txt (file) -> Xyz.zip (file) -> Address (Sub Directory) -> PWZ.log (file) -> CyZ.xml (file) -> DataLog.7zip 等 我试图做的是将这个
Folder - Directory (path -> /home/abc/xyz/Folder)
->Abc.txt (file)
-> Xyz.zip (file)
-> Address (Sub Directory)
-> PWZ.log (file)
-> CyZ.xml (file)
-> DataLog.7zip
等
我试图做的是将这个完整的目录从一个路径移动到另一个路径,包括所有文件和子文件夹(及其文件)
ie将此“文件夹”从/home/abc/xyz/文件夹移动到/home/abc/subdir/文件夹
Java是否提供了任何API来基于文件夹目录执行此任务,或者我们是否需要将每个文件递归复制到此路径 最好的方法可能是递归方法,如: 这是我创建的将文件移动到临时文件夹的方法
private boolean move(File sourceFile, File destFile)
{
if (sourceFile.isDirectory())
{
for (File file : sourceFile.listFiles())
{
move(file, new File(file.getPath().substring("temp".length()+1)));
}
}
else
{
try {
Files.move(Paths.get(sourceFile.getPath()), Paths.get(destFile.getPath()), StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException e) {
return false;
}
}
return false;
}
如果文件系统能够“移动”该文件,则可以工作。这通常要求您移动到同一磁盘上的不同位置。您只需使用
import static java.nio.file.StandardCopyOption.*;
Files.move(new File("C:\\projects\\test").toPath(), new File("C:\\projects\\dirTest").toPath(), StandardCopyOption.REPLACE_EXISTING);
更改源和目标路径
请参阅以获取更多详细信息
另请注意API
When invoked to move a
* directory that is not empty then the directory is moved if it does not
* require moving the entries in the directory. For example, renaming a
* directory on the same {@link FileStore} will usually not require moving
* the entries in the directory. When moving a directory requires that its
* entries be moved then this method fails (by throwing an {@code
* IOException}). To move a <i>file tree</i> may involve copying rather
* than moving directories and this can be done using the {@link
* #copy copy} method in conjunction with the {@link
* #walkFileTree Files.walkFileTree} utility method
调用以移动
*不为空的目录,如果不为空,则移动该目录
*需要移动目录中的条目。例如,重命名
*同一{@link FileStore}上的目录通常不需要移动
*目录中的条目。移动目录时,要求其
*如果无法移动条目,则此方法失败(通过抛出{@code
*IOException})。移动文件树可能需要复制而不是复制
*而不是移动目录,这可以使用{@link完成
*#copy copy}方法与{@link
*#walkFileTree文件.walkFileTree}实用程序方法
如果您试图在同一分区中移动文件,上面的代码就足够了(它可以移动目录,即使它有条目)。如果不是(而不是移动),您需要像前面提到的其他答案一样使用递归 如果您已经导入了:
FileUtils.moveDirectory(oldDir, newDir);
请注意,newDir
不能预先存在。从javadocs:
移动目录。
当目标目录位于另一个文件系统上时,请执行“复制并删除”
参数:srcDir-要移动的目录
destDir-目标目录
抛出:
NullPointerException-如果源或目标为null
FileExistsException-如果目标目录存在
IOException-如果源或目标无效
IOException-如果在移动文件时发生IO错误
Java使用本机OS操作在中内置了此功能 例如:
new File("/home/alik/Downloads/src").renameTo(new File("/home/alik/Downloads/target"))
在确保目标路径中的目录存在后(例如,通过调用
mkdirs()
),只需使用renameTo
方法(已存在于JDK的文件
类中)。私有静态无效移动(文件sourceFile,文件destFile){
if(sourceFile.isDirectory()){
File[]files=sourceFile.listFiles();
断言文件!=null;
对于(文件:文件)移动(文件,新文件(destFile,File.getName());
如果(!sourceFile.delete())抛出新的RuntimeException();
}否则{
如果(!destFile.getParentFile().exists())
如果(!destFile.getParentFile().mkdirs())抛出新的RuntimeException();
如果(!sourceFile.renameTo(destFile))抛出新的RuntimeException();
}
}
为我工作
public static void move(File srcDir, File destDir) throws IOException {
FileUtils.copyDirectory(srcDir, destDir, true);
if(srcDir.isDirectory()) {
for(File file: srcDir.listFiles()) {
FileUtils.forceDelete(file);
}
}
}
文件包org.apache.commons.io中的FileUtils 为什么不改为使用while循环;这是没有限制的。返回前可以进行的函数调用数量是有限制的。由于每个函数调用都允许操作系统在进入函数之前存储状态,请注意,apache commons在软符号链接方面存在问题。如果他们失败了,他们将在这里失败。考虑到他们试图尽可能与旧java兼容,这不太可能改变FileUtils。deleteDirectory(srcDir)应该递归地执行删除作业
public static void move(File srcDir, File destDir) throws IOException {
FileUtils.copyDirectory(srcDir, destDir, true);
if(srcDir.isDirectory()) {
for(File file: srcDir.listFiles()) {
FileUtils.forceDelete(file);
}
}
}