基于对以下问题的回答:List all files from a directory recursively with Java
我编写了一个小 Filemover,它将递归地移动目录中的每个文件并将它们放置在另一个目录的顶层。但由于某种原因,代码并未移动所有文件。我改用 Files.move() 但绝对值得,但代码仍然无法正确遍历目录树。现在,我在尝试移动文件时遇到 java.nio.file.NoSuchFileException 。
堆栈跟踪表明它的来源未知,但当我查看该文件是否存在时,它确实存在。我已将问题隔离到 Files.move() 但我似乎无法修复它。我尝试过 getPath() 和 getAbsolutePath()。更奇怪的是,我在排序例程中使用了类似的方法,而且效果很好。唯一的区别是我的源目录没有子目录。
我已经解决了部分树行走问题。这是因为我的 ImageFilter 只有小写扩展名,并且过滤器需要区分大小写。所以我在这里修复了它。
好的,我将我的小 Filemover 切换回 file.renameTo(),现在它可以正常工作了。它只是一个测试工具,用于将文件随机移动到图像分类器的放置目录中,因此不值得弄清楚为什么我会没有此类文件异常。我只是将其修改为递归工作,这样如果有人对一堆图像使用了错误的排序例程,它可以用于反转排序。
感谢您的帮助:)
我有3节课
import java.io.File;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import ca.fotonow.p3software.ImageFileFilter;
public class PSFileUtils {
public static Collection<File> listfiles(String directory) {
// TODO Auto-generated method stub
File dir= new File (directory);
dir.mkdirs(); //create directory if it doesn't exist.
Collection<File> files= FileUtils.listFiles(dir,new ImageFileFilter(),DirectoryFileFilter.DIRECTORY);
return files;
}
}
过滤器类
import java.io.File;
import java.io.FileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
public class ImageFileFilter implements IOFileFilter {
private final String filetypes[]= new String[] {"jpeg", "JPEG","jpg","JPG","tif","TIF","tiff","TIFF","raw","RAW"};
@Override
public boolean accept(File file) {
for (String ext: filetypes) {
if (file.getName().toLowerCase().endsWith(ext)) return true;
}
return false;
}
@Override
public boolean accept(File arg0, String arg1) {
// TODO Auto-generated method stub
return false;
}
}
主类
import java.io.File;
import java.util.Collection;
import java.util.Date;
import java.util.Random;
import java.nio.file.Files;
import static java.nio.file.StandardCopyOption.*;
public class FileMover {
/**
* @param args
*/
public static void main(String[] args) {
//Move random number of files from one directory args[0] to another directory args[1]
//directories can be relative
//Repeats randomly until files are gone
String dir1=args[0];
String dir2=args[1];
Collection<File> files=PSFileUtils.listfiles(dir1);
for (File file: files) {
File newfile=new File(dir2+"/"+file.getName());
try {
Files.move(file.toPath(),newfile.toPath(), REPLACE_EXISTING);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println((new Date()).getTime()+ " "+ newfile.getName());
Random generator =new Random(new Date().getTime());
try {
Thread.sleep(generator.nextInt(5000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
知道我做错了什么吗?
最佳答案
根据 javadocs,重命名文件可能不会移动文件:
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.
尝试使用java.nio.file.Files.move()
。
关于java - 将文件从一个目录递归移动到另一个目录仅部分完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16752025/