news 2026/5/30 13:50:27

Java五种文件拷贝方式

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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或缓冲流,平衡代码可读性与性能。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/25 13:09:50

验证回文串,x的平方根(左右指针)

这个题用暴力法会超时&#xff0c;使用左右指针。首先考虑如果不允许删除字符&#xff0c;如何判断一个字符串是否是回文串。常见的做法是使用双指针。定义左右指针&#xff0c;初始时分别指向字符串的第一个字符和最后一个字符&#xff0c;每次判断左右指针指向的字符是否相同…

作者头像 李华
网站建设 2026/5/26 19:11:53

ant design pro不安装第三方库,如何实现多标签页面(带源码)

在中后台管理系统开发场景中&#xff0c;动态标签页是提升用户操作体验的核心功能 —— 它模拟浏览器标签页交互逻辑&#xff0c;支持多页面并行操作、自由切换&#xff0c;还能保留用户的操作轨迹。本文将基于 React Umi&#xff08;umijs/max&#xff09; Ant Design 技术栈…

作者头像 李华
网站建设 2026/5/29 2:10:59

2025最新!研究生必备8个AI论文平台:开题报告与文献综述全测评

2025最新&#xff01;研究生必备8个AI论文平台&#xff1a;开题报告与文献综述全测评 2025年研究生必备AI论文平台测评&#xff1a;如何选择高效工具&#xff1f; 在科研日益数字化的今天&#xff0c;研究生群体对AI论文工具的需求愈发迫切。从开题报告到文献综述&#xff0c;从…

作者头像 李华
网站建设 2026/5/29 5:34:46

基于SpringBoot的图书管理系统的设计与实现毕业设计项目源码

项目简介 在图书馆数字化升级、借阅服务精细化需求下&#xff0c;传统图书管理存在 “借阅流程繁琐、库存盘点低效、读者画像缺失” 的痛点&#xff0c;基于 SpringBoot 构建的图书管理系统&#xff0c;适配读者、图书管理员、馆内运营人员等角色&#xff0c;实现图书借阅、馆藏…

作者头像 李华
网站建设 2026/5/20 14:25:22

2025最新!9款AI论文软件测评:本科生写论文必备神器

2025最新&#xff01;9款AI论文软件测评&#xff1a;本科生写论文必备神器 2025年AI论文工具测评&#xff1a;为何值得一看&#xff1f; 随着人工智能技术的不断进步&#xff0c;AI论文写作工具逐渐成为高校学生&#xff0c;尤其是本科生撰写学术论文的重要辅助手段。然而&…

作者头像 李华