桃园市网站建设_网站建设公司_百度智能云_seo优化
2025/12/24 22:54:54 网站建设 项目流程

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或缓冲流,平衡代码可读性与性能。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询