news 2026/4/15 17:06:59

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/4/15 22:51:31

提示词工程能够解决什么问题?

文章目录提示词工程能够解决什么问题&#xff1f;1.核心定位&#xff1a;提示词工程是用户意图与模型能力的桥梁2.解决的关键问题&#xff08;1&#xff09;任务定义模糊性&#xff08;2&#xff09;输出格式控制&#xff08;3&#xff09;领域知识适配&#xff08;4&#xff0…

作者头像 李华
网站建设 2026/4/8 0:19:09

基于SpringBoot+Vue的企业固定资产管理系统设计与实现

前言 &#x1f31e;博主介绍&#xff1a;✌CSDN特邀作者、全栈领域优质创作者、10年IT从业经验、码云/掘金/知乎/B站/华为云/阿里云等平台优质作者、专注于Java、小程序/APP、python、大数据等技术领域和毕业项目实战&#xff0c;以及程序定制化开发、文档编写、答疑辅导等。✌…

作者头像 李华
网站建设 2026/4/14 10:19:29

基于SpringBoot的深圳市体育中心体育赛事管理系统毕业设计项目源码

项目简介在大型体育场馆赛事运营精细化、数字化需求下&#xff0c;深圳市体育中心传统赛事管理存在 “流程割裂、资源调度低效、数据统计滞后” 的痛点&#xff0c;基于 SpringBoot 构建的赛事管理系统&#xff0c;适配赛事运营人员、场馆管理员、参赛人员、观众等角色&#xf…

作者头像 李华
网站建设 2026/4/13 14:08:02

Windows系统文件rpcnsh.dll缺少损坏问题 下载修复方法

在使用电脑系统时经常会出现丢失找不到某些文件的情况&#xff0c;由于很多常用软件都是采用 Microsoft Visual Studio 编写的&#xff0c;所以这类软件的运行需要依赖微软Visual C运行库&#xff0c;比如像 QQ、迅雷、Adobe 软件等等&#xff0c;如果没有安装VC运行库或者安装…

作者头像 李华