当前位置: 首页 > news >正文

Java五种文件拷贝方式

Java五种文件拷贝方式

  • 在Java中,文件拷贝主要有以下几种方式,不同场景下效率差异显著;
  • 以下从实现方式,效率对比和适用场景三方面详情解析。

文件拷贝的5种实现方式

1.传统字节流(FileInputStream+FileOutputStream)

/** * 传统字节流(FileInputStream+FileOutputStream) * @throws IOException */publicstaticvoidcopy1()throwsIOException{StringsourcePath="C:\\Users\\Administrator\\Desktop\\interview\\Code\\src\\main\\resources\\source.txt";StringtargetPath="C:\\Users\\Administrator\\Desktop\\interview\\Code\\src\\main\\resources\\target.txt";InputStreaminputStream=newFileInputStream(sourcePath);OutputStreamoutputStream=newFileOutputStream(targetPath);byte[]buffer=newbyte[1024];intlength;while((length=inputStream.read(buffer))>0){outputStream.write(buffer,0,length);}}
  • 特点:基础方式,直接逐字节或缓冲区读写;
  • 效率:最低(适合小文件)。

2.缓冲流优化拷贝(BufferedInputStream+BufferedOutputStream)

/** * 缓冲流优化拷贝(BufferedInputStream+BufferedOutputStream) * @throws IOException */publicstaticvoidcopy2()throwsIOException{StringsourcePath="C:\\Users\\Administrator\\Desktop\\interview\\Code\\src\\main\\resources\\source.txt";StringtargetPath="C:\\Users\\Administrator\\Desktop\\interview\\Code\\src\\main\\resources\\target.txt";BufferedInputStreaminputStream=newBufferedInputStream(newFileInputStream(sourcePath));BufferedOutputStreamoutputStream=newBufferedOutputStream(newFileOutputStream(targetPath));byte[]buffer=newbyte[8192];//缓冲区大小,缓冲区越大,性能越好(通常8KB~64KB)intlength;while((length=inputStream.read(buffer))>0){outputStream.write(buffer,0,length);}}
  • 特点:通过减少缓冲区I/O次数;
  • 效率:比传统字节流提升2~5倍。

3.NIO Files.copy(Java7+)

/** * NIO Files.copy(Java7+) * @throws IOException */publicstaticvoidcopy3()throwsIOException{StringsourcePath="C:\\Users\\Administrator\\Desktop\\interview\\Code\\src\\main\\resources\\source.txt";StringtargetPath="C:\\Users\\Administrator\\Desktop\\interview\\Code\\src\\main\\resources\\target.txt";Files.copy(newFile(sourcePath).toPath(),newFile(targetPath).toPath());}
  • 特点:单行代码完成拷贝,底层自动优化;
  • 效率:接近最高效(适合大多数场景)。

4.NIO FileChannel通道拷贝

/** * NIO Files.copy(Java7+) * @throws IOException */publicstaticvoidcopy4()throwsIOException{StringsourcePath="C:\\Users\\Administrator\\Desktop\\interview\\Code\\src\\main\\resources\\source.txt";StringtargetPath="C:\\Users\\Administrator\\Desktop\\interview\\Code\\src\\main\\resources\\target.txt";FileChannelsourceChannel=newFileInputStream(sourcePath).getChannel();FileChanneltargetChannel=newFileOutputStream(targetPath).getChannel();sourceChannel.transferTo(0,sourceChannel.size(),targetChannel);}
  • 特点:利用通道(Channel)直接传输数据;
  • 效率:大文件性能最佳(利用零拷贝技术)。

5.内存映射文件拷贝(MappedByBuffer)

/** * 内存映射文件拷贝(MappedByBuffer) * @throws IOException */publicstaticvoidcopy5()throwsIOException{StringsourcePath="C:\\Users\\Administrator\\Desktop\\interview\\Code\\src\\main\\resources\\source.txt";StringtargetPath="C:\\Users\\Administrator\\Desktop\\interview\\Code\\src\\main\\resources\\target.txt";RandomAccessFilesourceChannel=newRandomAccessFile(sourcePath,"r");RandomAccessFiletargetChannel=newRandomAccessFile(targetPath,"rw");FileChannelchannel=targetChannel.getChannel();MappedByteBuffermappedByteBuffer=sourceChannel.map(FileChannel.MapMode.READ_ONLY,0,sourceChannel.size());targetChannel.getChannel().write(mappedByteBuffer);}
  • 特点:将文件映射到内存中直接操作;
  • 效率:适合超大文件(但实现复杂,需要谨慎处理内存)。

效率对比(1GB文件测试)

方法耗时(毫秒)适用场景
传统字节流4500~5000小文件(<10MB)
缓冲流1200~1500通用场景
Files.copy800~1000简单代码 + 快速开发
FileChannel.transfer600~800大文件(>100MB)
内存映射文件500~700超大文件(>1GB)

如何选择最高效的方式

  1. 小文件(<10MB):直接使用File.copy,代码简洁且性能足够;
  2. 大文件(100MB~1GB):优先选择FileChannel.transferTo(),利用零拷贝减少内核态与用户态数据复制;
  3. 超大文件(>1GB): 用内容映射文件(MappedByteBuffer)
    1. 但需要注意避免频繁映射/释放资源(开销大);
    2. 处理内容溢出风险(OutOfMemeory)。
  4. 通用场景:File.copy或缓冲流,平衡代码可读性与性能。
http://www.jsqmd.com/news/135632/

相关文章:

  • 2-[(2-叠氮乙酰基)氨基]-2-脱氧-D-吡喃甘露糖—糖生物学与代谢标记的关键化学探针 1971934-97-0
  • 萤石开放平台 国标设备接入 |常见问题
  • 计算机Java毕设实战-基于springboot+vue技术的二手车交易管理系统的设计与实现基于SpringBoot+Vue的二手车交易平台设计【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • 总结
  • 提示词工程能够解决什么问题?
  • 反射2-获取class对象的三种方式
  • 新品限免|国产大模型工程化实战:GLM-4.7与MiniMax M2.1 免费选型对比
  • Java毕设项目:基于springboot+vue技术的二手车交易管理系统的设计与实现(源码+文档,讲解、调试运行,定制等)
  • 电信公网IPV4被收回之后:家庭网络的“绝地求生”折腾记
  • Java毕设选题推荐:基于Java的停车场管理系统【附源码、mysql、文档、调试+代码讲解+全bao等】
  • HR 年终总结 PPT 工具测评:哪款最适合人事岗位?
  • 时序数据库 IoTDB 2.0 双模型架构详解:树、表如何融合,查询如何进化?
  • python自定义注解
  • Java毕设选题推荐:基于SpringBoot的植物知识管理与分享平台的设计与实现基于SpringBoot的植物知识分享系统的设计与实现【附源码、mysql、文档、调试+代码讲解+全bao等】
  • C#文件操作指南
  • python基于Vue的客户关系管理系统的设计与实现_3itcvt88
  • 12/24第五章
  • Java计算机毕设之基于SpringBoot的二手车交易平台设计与实现基于springboot+vue技术的二手车交易管理系统的设计与实现(完整前后端代码+说明文档+LW,调试定制等)
  • TCP通信练习5-上传文件(多线程)
  • 自适应滤波算法的FPGA实现思路
  • Pandoc转换Word文档:使用Lua过滤器统一调整Pandoc文档中的图片和表格格式
  • 别再自己苦熬写论文了!8款免费AI神器30分钟搞定,文理医工全覆盖
  • vue基于python的民宿房间预订推荐系统的设计与实现_7r8s9b63(pycharm django flask)
  • hive3之数据倾斜解决方案
  • Java毕设选题推荐:基于springboot的美发商城系统基于springboot美发门店管理系统设计与实现【附源码、mysql、文档、调试+代码讲解+全bao等】
  • VFF-Net:一种取代反向传播的AI训练新算法
  • Java计算机毕设之基于Java的停车场管理系统(完整前后端代码+说明文档+LW,调试定制等)
  • Java毕设项目推荐-基于springboot+vue技术的二手车交易管理系统的设计与实现基于SpringBoot的二手车交易平台设计与实现【附源码+文档,调试定制服务】
  • springboot-vue企业会议交换机设备维修批量运维管理系统 vue没论文py09
  • vue基于python的物业维修服务预约平台_g310h596(pycharm django flask)