java 复制目录,使用Java将文件从一个目录复制到另一个目录

该博客介绍如何使用Java将dir目录中的前20个文本文件复制到名为trainingData的子目录中,涉及`FileUtils.copyDirectory`方法和第三方库Apache Commons IO。作者提到使用mkdir()创建目标目录并避免重复编写工具。
摘要由CSDN通过智能技术生成

I want to copy files from one directory to another (subdirectory) using Java. I have a directory, dir, with text files. I iterate over the first 20 files in dir, and want to copy them to another directory in the dir directory, which I have created right before the iteration.

In the code, I want to copy the review (which represents the ith text file or review) to trainingDir. How can I do this? There seems not to be such a function (or I couldn't find). Thank you.

boolean success = false;

File[] reviews = dir.listFiles();

String trainingDir = dir.getAbsolutePath() + "/trainingData";

File trDir = new File(trainingDir);

success = trDir.mkdir();

for(int i = 1; i <= 20; i++) {

File review = reviews[i];

}

解决方案

For now this should solve your problem

File source = new File("H:\\work-temp\\file");

File dest = new File("H:\\work-temp\\file2");

try {

FileUtils.copyDirectory(source, dest);

} catch (IOException e) {

e.printStackTrace();

}

FileUtils class from apache commons-io library, available since version 1.2.

Using third party tools instead of writing all utilities by ourself seems to be a better idea. It can save time and other valuable resources.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值