java文件拷贝的集中方式

server/2024/9/23 5:23:13/

四种拷贝方式

  1. 不同io
  2. buffer io
  3. mmap 零拷贝
  4. sendfile 零拷贝

核心代码

java">package com.itzhongzi.io;import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;/*** @author Administrator* @description* @since 2024-04-23 19:30* <p>* 运行方式: java IOTest.java "io" "source" "target"*/
public class IOTest {public static void main(String[] args) {String type = args[0];String inputFilePath = args[1];String outputFilePath = args[2];if ("io".equalsIgnoreCase(type)) {inputStreamCopyFile(inputFilePath, outputFilePath);} else if ("buffer".equalsIgnoreCase(type)) {bufferInputStreamCopyFile(inputFilePath, outputFilePath);} else if ("mmap".equalsIgnoreCase(type)) {mmapInputStreamCopyFile(inputFilePath, outputFilePath);} else if ("sendfile".equalsIgnoreCase(type)) {sendfileInputStreamCopyFile(inputFilePath, outputFilePath);}}/*** 普通io** @param inputFilePath* @param outputFilePath*/private static void inputStreamCopyFile(String inputFilePath, String outputFilePath) {long start = System.currentTimeMillis();try (FileInputStream fis = new FileInputStream(inputFilePath);FileOutputStream fos = new FileOutputStream(outputFilePath)) {byte[] buf = new byte[1024];int len;;while ((len = fis.read(buf)) != -1) {fos.write(buf);}} catch (Exception e) {e.printStackTrace();}long end = System.currentTimeMillis();System.out.println("耗时" + (end - start) + "ms");}/*** buffer io** @param inputFilePath* @param outputFilePath*/private static void bufferInputStreamCopyFile(String inputFilePath, String outputFilePath) {long start = System.currentTimeMillis();try (BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(Paths.get(inputFilePath)));BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(Paths.get(outputFilePath)));) {byte[] buf = new byte[1024];int len;while ((len = bis.read(buf)) != -1) {bos.write(buf);}} catch (Exception e) {e.printStackTrace();}long end = System.currentTimeMillis();System.out.println("耗时" + (end - start) + "ms");}/*** mmap 零拷贝io** @param inputFilePath* @param outputFilePath*/private static void mmapInputStreamCopyFile(String inputFilePath, String outputFilePath) {try (FileChannel channelIn = new FileInputStream(inputFilePath).getChannel();FileChannel channelOut = new RandomAccessFile(outputFilePath, "rw").getChannel();) {long size = channelIn.size();System.out.println("mmap size:" + size);MappedByteBuffer mappedInByteBuffer = channelIn.map(FileChannel.MapMode.READ_ONLY, 0, size);MappedByteBuffer mappedOutByteBuffer = channelOut.map(FileChannel.MapMode.READ_WRITE, 0, size);for (int i = 0; i < size; i++) {mappedOutByteBuffer.put(mappedInByteBuffer.get(i));}} catch (Exception e) {e.printStackTrace();}}/*** sendfile 零拷贝io** @param inputFilePath* @param outputFilePath*/private static void sendfileInputStreamCopyFile(String inputFilePath, String outputFilePath) {try (FileChannel channelIn = new FileInputStream(inputFilePath).getChannel();FileChannel channelOut = new FileOutputStream(outputFilePath).getChannel()// FileChannel channelOut = new RandomAccessFile(outputFilePath, "rw").getChannel();) {// 方式一// 针对小于 2G 文件,超过 2G,超出部分会丢弃// long transferSize = channelIn.transferTo(0, channelIn.size(), channelOut);// 方式二: 针对大于2G的文件,分多次读写// 获取文件总大小long size = channelIn.size();for (long left = size; left > 0; ) {// 真实拷贝的长度, size - left 计算下次拷贝的位置long transferSize = channelIn.transferTo(size - left, left, channelOut);left = size - transferSize;}} catch (Exception e) {e.printStackTrace();}}}

http://www.ppmy.cn/server/11830.html

相关文章

开发环境中的调试视图(IDEA)

当程序员写完一个代码时必然要运行这个代码&#xff0c;但是一个没有异常的代码却未必满足我们的要求&#xff0c;因此就要求程序员对已经写好的代码进行调试操作。在之前&#xff0c;如果我们要看某一个程序是否满足我们的需求&#xff0c;一般情况下会对程序运行的结果进行打…

「51媒体」企业参加展览展会,媒体直播宣传需要准备什么?

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 企业参加展览展会并进行媒体直播宣传时&#xff0c;需要准备以下几个方面的事项&#xff1a; 确定展会信息&#xff1a; &#xff08;1&#xff09; 展会的主题、时间、地点。 &#…

支付宝支付之SpringBoot整合支付宝入门

文章目录 支付宝支付对接流程核心所需的参数申请注册配置开发信息接口加签方式 发起支付订单案例门店直连方式商家/服务商后台转发方式安全设计支付pom.xmlapplication.ymlAlipayController.java统一收单线下交易业务请求参数CURL请求示例实例代码 查询撤销重要入参说明重要出参…

SQL注入作业

目录 一、万能密码和二阶注入测试 1.万能密码 2.二阶注入测试 二、联合查询注入测试 1.判断注入点 2.判断当前查询语句的列数 3.查询数据库基本信息 4.查询数据库中的数据 三、报错注入 1. 报错注入函数EXTRATVALUE 2.UPDATEXML 四、盲注测试 1.布尔盲注 判断数据…

冈萨雷斯数字图像处理资源(课后习题答案+代码+图片)

冈萨雷斯数字图像处理相关资源整理&#xff0c;资源全部来源互联网&#xff0c;方便大家下载 冈萨雷斯数字图像处理相关资源整理 课后习题 冈萨雷斯数字图像处理源代码

ReclaiMe Pro:丢失分区的恢复方法

天津鸿萌科贸发展有限公司是 ReclaiMe Pro 数据恢复软件的授权代理商。 ReclaiMe Pro 的分区恢复功能 ReclaiMe Pro 提供专业的分区恢复功能&#xff0c;支持从各种文件系统中搜索丢失和损坏的分区&#xff0c;例如 FAT、exFAT、NTFS、EXT、XFS 和 VMFS。 ReclaiMe Pro 启动…

科研基础与工具(论文写作)

免责申明&#xff1a; 本文内容只是学习笔记&#xff0c;不代表个人观点&#xff0c;希望各位看官自行甄别 参考文献 科研基础与工具&#xff08;YouTube&#xff09; 学术写作句型 Academic Phrase bank 曼彻斯特大学维护的一个网站 写论文的时候&#xff0c;不不知道怎么…

JavaScript系列:实用的数组比较方法

目录 引用比较的局限性 使用JSON.stringify()方法比较 使用Array.every()方法进行比较 结论 在JavaScript编程中&#xff0c;经常会遇到需要比较两个数组是否相等的场景。但是&#xff0c;由于JavaScript中数组是以引用方式比较&#xff0c;而非值比较&#xff0c;这使得直…