使用Java在通用io中使用moveFile方法移动文件的异常 [英] Exception in moving a file using moveFile method in common io using java

查看:1168
本文介绍了使用Java在通用io中使用moveFile方法移动文件的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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...");

上面的代码抛出异常

"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' "

并且我在将文件复制到目标位置后尝试删除该文件,但无法删除.

我尝试使用deleteOnExit()方法而不是delete(),但没有任何效果.

我已经使用md5算法来检查两个文件的相似性. 如果文件不相同,我想将文件移到目标目录.

解决方案

从上面的代码看来,您似乎要将一个文件从一个目录移动到另一个目录. 根据此假设,您可以使用以下代码.

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...");

这肯定会对您有所帮助.

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.

这篇关于使用Java在通用io中使用moveFile方法移动文件的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆