拷贝一个文件的算法比较简单,当然,可以对它进行优化,比如使用缓冲流,提高读写数据的效率等。

话不多说直接上代码

import java.io.*;

/**
* 实现文件的拷贝
*/
public class CopyFile { /**
* 复制文件夹
*
* @param resource 源路径
* @param target 目标路径
*/
public static void copyFolder(String resource, String target) throws Exception { File resourceFile = new File(resource);
if (!resourceFile.exists()) {
throw new Exception("源目标路径:[" + resource + "] 不存在...");
}
File targetFile = new File(target);
if (!targetFile.exists()) {
throw new Exception("存放的目标路径:[" + target + "] 不存在...");
} // 获取源文件夹下的文件夹或文件
File[] resourceFiles = resourceFile.listFiles(); for (File file : resourceFiles) { File file1 = new File(targetFile.getAbsolutePath() + File.separator + resourceFile.getName());
// 复制文件
if (file.isFile()) {
System.out.println("文件" + file.getName());
// 在 目标文件夹(B) 中 新建 源文件夹(A),然后将文件复制到 A 中
// 这样 在 B 中 就存在 A
if (!file1.exists()) {
file1.mkdirs();
}
File targetFile1 = new File(file1.getAbsolutePath() + File.separator + file.getName());
copyFile(file, targetFile1);
}
// 复制文件夹
if (file.isDirectory()) {// 复制源文件夹
String dir1 = file.getAbsolutePath();
// 目的文件夹
String dir2 = file1.getAbsolutePath();
copyFolder(dir1, dir2);
}
} } /**
* 复制文件
*
* @param resource
* @param target
*/
public static void copyFile(File resource, File target) throws Exception {
// 输入流 --> 从一个目标读取数据
// 输出流 --> 向一个目标写入数据 long start = System.currentTimeMillis(); // 文件输入流并进行缓冲
FileInputStream inputStream = new FileInputStream(resource);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); // 文件输出流并进行缓冲
FileOutputStream outputStream = new FileOutputStream(target);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); // 缓冲数组
// 大文件 可将 1024 * 2 改大一些,但是 并不是越大就越快
byte[] bytes = new byte[1024 * 2];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, len);
}
// 刷新输出缓冲流
bufferedOutputStream.flush();
//关闭流
bufferedInputStream.close();
bufferedOutputStream.close();
inputStream.close();
outputStream.close(); long end = System.currentTimeMillis(); System.out.println("耗时:" + (end - start) / 1000 + " s"); } // 使用示例
public static void main(String[] args) { String rootPath = LoggerUtil.getJarRootPath();
// rootPath = "E:\MyProject\student\target\classes"; System.out.println("--------------------------------复制文件-------------------------------------------"); File f1 = new File("D:\\GHO\\Windows10企业版.iso");
// 目标文件
File f2 = new File("F:\\logs\\" + "win10.iso"); try {
// 这个 win10系统 大概是 3.50G 的 复制过程 花了 156 秒 == 2 分6 秒
copyFile(f1, f2);
} catch (Exception e) {
e.printStackTrace();
} System.out.println("--------------------------------复制文件夹-------------------------------------------"); String resource = rootPath + "logs" + File.separator + "job1234";
String target = rootPath + "logs" + File.separator + "job123";
try {
copyFolder(resource, target);
} catch (Exception e) {
e.printStackTrace();
} } }

最新文章

  1. solr DIH 知识梳理
  2. IOS RunLoop面试题
  3. 继续OI
  4. 支付宝Payto接口的C#.net实现方法
  5. 原生Ajax写法(GET)
  6. JAVA设计模式之桥梁模式
  7. 【转】【C#】C#重绘windows窗体标题栏和边框
  8. TYVJ P1020 寻找质因数
  9. BEvent_客制化BusinessEvent通过PLSQL Procedurer接受消息传递(案例)
  10. 触摸点为scrollview上的子控件时,scrollview不能滚动(iOS8)
  11. FFmpeg命令行工具学习(五):FFmpeg 调整音视频播放速度
  12. 通过 iis或者本地IP 调试代码
  13. 当一些库和类无法在Silverlight工程中使用
  14. sqlmap实例文档
  15. [干货教程]仿网易云课堂微信小程序开发实战经验
  16. 远程链接 aws Windows Server 2016 Base Nano
  17. 安装Numpy方法
  18. tomcat conf目录下文件的作用
  19. Wavesurfer.js音频播放器插件的使用教程
  20. Linux下安装Nginx详细图解教程(一)

热门文章

  1. 2019华东交通大学ACM基地简介
  2. Jquery中数组转字符串,c:foreach自动将带","字符串进行拆分赋值
  3. spring mvc 简单实现及相关配置实现
  4. 数据分析—win7+ipython+notebook安装
  5. 20190806-Python基础 第二章 列表和元组(3)元组&章小结
  6. ASP.NET-A low-level Look at the ASP.NE
  7. Eclipse MyEclipse 反编译.class文件 myeclipse source not found
  8. CF336C-Vasily the Bear and Sequence题解--贪心
  9. Python 数字(函数)
  10. 【Struts2】拦截器