java copy directorytodirectory 替换,Java - Copy file to another directory using FileUtils and copyFile...

问题

I would like to copy a file from one directory to another using Java and the FileUtils classes of apache org commons.

I wrote up a quick java program to test on my local system. Here is the code. The file exists, but the copying of the file to another directory isn't working. What am I missing? Is there some improper syntax somewhere?

import org.apache.commons.io.FileUtils;

import java.io.File;

class MoveFile {

public static void main(String[] args) {

MoveFile myobj = new MoveFile();

myobj.moveTheFile();

}

public void moveTheFile () {

try {

File destDir = new File("C:\\Folder1\\temp2");

File srcFile = new File("C:\\Folder1\\temp\\card.png");

FileUtils.copyFileToDirectory(srcFile, destDir);

} catch(Exception e) {

}

}

}

回答1:

Replicated your error and it only fails when the program does not have permission to write on destination folder. Even catching a throwable and printing stacktrace shows no info and the method is quite silent... if the folder does not exist, the method creates it so disregard that possible correction.

Check write permissions in destination folder

回答2:

It is never a good idea to do swallow exceptions. Do an e.printstacktrace() in your exception handling mechanism for more information. Since you did not specify any other information, first thing that comes to mind is that if you are using Windows vista or later, usually it will ask you for administrator consent when placing items directly in your C:\ directory.

To see if this is the problem, I would recommend you test this out in other directories such as My Documents or else, disable the UAC.

回答3:

Do

destDir.mkdirs();

or

FileUtils.forceMkdir(destDir);

to create the directory temp2 first.

来源:https://stackoverflow.com/questions/9235401/java-copy-file-to-another-directory-using-fileutils-and-copyfiletodirectory

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值