黑马程序员技术交流社区
标题:
将一个文件夹下所有子文件夹和文件复制到另一个文件夹下
[打印本页]
作者:
Coolman
时间:
2015-9-12 00:47
标题:
将一个文件夹下所有子文件夹和文件复制到另一个文件夹下
将文件夹a下(包括其下所有子文件夹和文件)复制到文件夹b下。
涉及知识点:字节流,缓冲字节流,文件操作,foreach循环,字符串操作,正则表达式,异常捕获处理
实现步骤:
* 复制单级文件夹
* 1.在目标路径创建和源路径同名文件夹
* 2.遍历源路径下的文件,并进行复制
* 3.改进功能:复制多级文件夹
* 4.增加复制文件后改名的功能
注:代码未经调优简化,方便理解,同一功能实现方式可能有多种,此案例仅供参考交流
package cn.itcast;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
/*
* 复制单级文件夹
* 1.在目标路径创建和源路径同名文件夹
* 2.遍历源路径下的文件,并进行复制
* 3.改进功能:复制多级文件夹
* 4.增加复制文件后改名的功能
*/
public class CopyFileFolder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入源路径");
String sour = sc.nextLine();
sour.replace("\\", "\\\\");
System.out.println("请输入目标路径");
String dest = sc.nextLine();
dest.replace("\\", "\\\\");
sc.close();
long start = System.currentTimeMillis();
copyFolder(new File(sour), new File(dest));
System.out.println("复制完成!!");
long end = System.currentTimeMillis();
System.out.println("总共用时 " + (end - start) + " ms");
}
// 传递源路径和目标路径
public static void copyFolder(File sourPath, File destPath) {
// 获取源文件夹名
String folderName = sourPath.getName();
// 根据目标路径和源文件夹名创建file对象
File destFolder = new File(destPath, folderName);
// 创建目标文件夹
destFolder.mkdirs(); // I:\Knownsec
System.out.println("创建文件夹 " + destFolder.getName());
// 遍历源路径获取所有文件名和文件夹名
File[] files = sourPath.listFiles();
for (File file : files) {
// 判断如果是文件,获取文件名,进行复制操作
if (file.isFile()) {
// 获取文件名字符串形式
String fileName = file.getName();
// System.out.println(fileName);
// 根据目标父路径和源文件名创建file对象
File destFile = new File(destFolder, fileName);
// 传递源文件对象和目标文件对象进行复制操作
copyFile(file, destFile);
rename(destFile);
} else { // 是文件夹,递归调用本函数
// 此时,源路径为当前文件夹名,目标路径为刚才创建好的目标路径下的文件夹
copyFolder(file, destFolder);
}
}
}
// 对文件进行重命名
public static void rename(File destFile) {
// 获取文件父路径
File parent = destFile.getParentFile();
// 对文件名进行切割,方便获取文件扩展名
String[] name = destFile.getName().split("\\.");
// 将文件名扩展名替换为新的扩展名
String child = destFile.getName().replace(name[name.length - 1], "bak");
// 根据父路径和新文件名构造新文件对象,
File destNew = new File(parent, child);
// 传递改名后的新文件对象
destFile.renameTo(destNew);
}
// 利用缓冲字节流实现读写字节数组实现复制文件
public static void copyFile(File file, File destFile) {
// 声明缓冲字节流对象
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 创建缓冲字节流对象,封装字节流对象
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(new FileOutputStream(destFile));
long start = System.currentTimeMillis();
byte[] b = new byte[1024];
int len = 0;
System.out.println("正在复制... " + file.getName() + " 文件大小约 " + (file.length()/1024) + " KB");
// 利用字节数组复制读写字节流
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
long end = System.currentTimeMillis();
System.out.println("用时 " + (end - start) + " ms");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("复制失败");
} finally {
try {
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭资源失败");
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭资源失败");
}
}
}
}
}
}
复制代码
CopyFileFolder.zip
2015-9-12 00:47 上传
点击文件名下载附件
1.61 KB, 下载次数: 33
CopyFileFolder.java
作者:
13414309233F
时间:
2015-9-12 02:09
仰望中。。。。。赞!!!!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2