【发布时间】:2014-07-25 13:11:22
【问题描述】:
我想将文件从父目录复制到父目录的子文件夹中。现在我将复制的文件放入子文件夹,但如果我已经复制了子文件夹和文件,它每次都会重复,它会一直重复,我希望它只男性一次
public static void main(String[] args) throws IOException {
File source = new File(path2);
File target = new File("Test/subfolder");
copyDirectory(source, target);
}
public static void copyDirectory(File sourceLocation, File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i = 0; i < children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]), new File(
targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
byte[] buf = new byte[1];
int length;
while ((length = in.read(buf)) > 0) {
out.write(buf, 0, length);
}
in.close();
out.close();
}
}
【问题讨论】:
-
请重新表述您的问题和解释。很难理解。
-
显示here可能会解决您的问题。
-
在进行复制之前尝试验证 else 语句中是否存在“targetLocation”文件。
标签: java