Based on an answer to: List all files from a directory recursively with Java
I have written a little Filemover which will recursively move every file from a directory and place them in the top level of another directory. But for some reason, the code doesn't move all the files. I switched to using Files.move() but while definitely worth while the code is still not walking the directory tree properly. Now I am getting a java.nio.file.NoSuchFileException while trying to move the files.
The stack trace says that its an unknown source, yet when I look to see if the file is there it is. I have isolated the problem to Files.move() but I can't seems to fix it. I have tried both getPath() and getAbsolutePath(). What's even weirder I use a similar method in my sorting routine and it works fine. The only difference is that my source directory has no subdirectories.
I have solved the partial tree walk. It was caused because my ImageFilter only had lower case extensions and the filter needed to be case sensitive. So I fixed it here.
Okay, I switched my little Filemover back to file.renameTo() and it works properly now. It's just a testing tool for randomly moving files into a drop directory for my image sorter so it's not worth figuring out why I was getting no such file exceptions. I was just modifying it to work recursively so it could be used to reverse a sort if someone used the wrong sorting routine on a bunch of images.
Thanks for all your help :)
I have 3 classes
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;
}
}
Filter Class
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;
}
}
The main class
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();
}
}
}
}
Any idea of what I have wrong?