File source=new File(fname1);
System.out.println("souce name "+fname1);
File dest = new File("F:\\BackupFiles",source.getName());
try
{
FileUtils.moveFile(source, dest);
source.delete();
}
catch (IOException ex)
{
Logger.getLogger(FileCompare.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("file moved successfully...");
the above code throws exception
"java.io.IOException: Failed to delete original file 'C:\xampp\htdocs\eyeOS\eyeos\users\ajkani\files\html.txt' after copy to 'F:\BackupFiles\html.txt' "
and i tried to delete the file after copied it to the destination but unable to delete.
i tried deleteOnExit() method instead of delete() but nothing works.
i have used md5 algorithm to check the similarity of two files.
if the files are not same.i want to move the files to destination directory.
解决方案
From above code, it seems that you want to move one file from one directory to another.
As per this assumption, you can use below code.
String sourcePath = "D:\\other\\new.xls";
File source = new File(sourcePath);
System.out.println("souce name " + sourcePath);
File destDirPath = new File("D:\\");
try {
FileUtils.moveFileToDirectory(source, destDirPath, false);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("file moved successfully...");
This will definitely help you.