2
votes
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.

1
It seems that you don't have the write right. - sp00m
Seems like your source file is locked by another process. - vzamanillo
either you don't have sufficient permissions to delete the source file, or it's locked by some other process (maybe your previous java attempts, or some editor). I'd use a tool such as Unlocker to check for it. - Pelit Mamani
please say the tool name to unlock. - user3382504

1 Answers

0
votes

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.