本帖最后由 #→_→ 于 2015-9-21 00:00 编辑
public static void main(String[] args) throws IOException {
File yuan = new File("E:\\KuGou");// 源路径
File muDi = new File("E:\\demo\\KuGou");// 目的路径
muDi.mkdirs();// 创建目的路径
fuZhi(yuan, muDi);
}
private static void fuZhi(File yuan, File muDi) throws IOException {
File[] file = yuan.listFiles();// 创建数组和遍历数组
for (File newYuan : file) {
// 如果是文件就复制,是文件夹就再创建数组遍历
if (!newYuan.isDirectory()) {
// 数组的File路径作为新的源目录路径
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(newYuan));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(muDi + "\\"
+ newYuan.getName()));
byte[] by = new byte[1024];
int len;
while ((len = bis.read(by)) != -1) {
bos.write(by, 0, len);
}
bis.close();
bos.close();
} else {
// 创建下一级的新目录,并把新目录重新遍历复制
File newMuDi = new File(muDi, newYuan.getName());
newMuDi.mkdirs();
fuZhi(newYuan, newMuDi);
}
}
}
|
|