我试图从文件夹中找到最新的10个文件,然后将其移动到另一个文件夹,我能够找到最新的10个文件,但文件不会移动到另一个文件夹,renameTo总是返回false并且可以写入返回true。
可能的原因和解决方案是什么?
class MoveFiles
{
int numOfForms;
String source;
String destination;
static String finalDest;
String stateName;
MoveFiles(int numOfForms,String source,String destination, String stateName)
{
System.out.println("Move Files Constructor");
this.numOfForms=numOfForms;
this.source=source;
this.destination=destination;
this.stateName=stateName;
}
public void mFile()
{
System.out.println("Starting moving files");
finalDest = "C:\\PDF\\"+destination+"\\"+stateName;
File f = new File(finalDest);
System.out.println("Can Write ? : "+f.canWrite());
System.out.println(f.mkdirs());
for ( int i=0;i<10;i++)
{
File moveit = MoveFiles.lastFileModified(source);
System.out.println(moveit.renameTo(f));
}
}
public static File lastFileModified(String dir) {
File fl = new File(dir);
File[] files = fl.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
});
long lastMod = Long.MIN_VALUE;
File choice = null;
for (File file : files) {
if (file.lastModified() > lastMod) {
choice = file;
lastMod = file.lastModified();
}
}
return choice;
}
}