java file move,使用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...");

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值