java文件拷贝的集中方式

四种拷贝方式

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

核心代码

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.mfbz.cn/a/569789.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

YashanDB V23.2 LTS发版 | 共享集群首个长期支持版本

4月&#xff0c;YashanDB正式发布长期支持版本YashanDB V23.2 LTS&#xff0c;标志着YashanDB单机主备、共享集群和分布式实时数仓等完整产品体系&#xff0c;已全面进入可规模化使用的长期支持阶段&#xff1b;同时配套数据迁移工具、监控运维工具和开发者工具&#xff0c;可以…

串口服务器和光纤交换机的区别

串口服务器与光纤交换机在功能和应用上存在显著区别。串口服务器主要实现串口设备与以太网设备之间的数据转换与传输&#xff0c;适用于远程监控、数据采集等场景&#xff1b;而光纤交换机则专注于高速光纤网络中的数据交换&#xff0c;为大型企业或数据中心提供稳定、高效的数…

基于Google Gemini 探索大语言模型在医学领域应用评估和前景

概述 近年来&#xff0c;大规模语言模型&#xff08;LLM&#xff09;在理解和生成人类语言方面取得了显著的飞跃&#xff0c;这些进步不仅推动了语言学和计算机编程的发展&#xff0c;还为多个领域带来了创新的突破。特别是模型如GPT-3和PaLM&#xff0c;它们通过吸收海量文本…

LearnOpenGL(四)之纹理

一、纹理 纹理是一个2D图片&#xff08;甚至也有1D和3D的纹理&#xff09;&#xff0c;它可以用来为每个顶点添加颜色来增加图形的细节&#xff0c;从而创建出有趣的图像。 纹理坐标在x和y轴上&#xff0c;范围为0到1之间&#xff08;注意我们使用的是2D纹理图像&#xff09;…

Maven如何解决jar包冲突的问题?

在使用Maven进行项目构建的应用中&#xff0c;如果在应用运行期发生了NoSuchMethodError、ClassNotFoundException等异常或者错误时&#xff0c;需要考虑Jar包冲突的问题。 如果在应用中&#xff0c;我们同时依赖了两个第三方的jar包A&#xff0c;B&#xff0c;而A&#xff0c;…

制造企业如何打造客户服务核心竞争力?[AMT企源典型案例]

引言 产品同质化严重&#xff0c;竞争的焦点从产品转向服务&#xff0c;企业的管理模式也要相应转变。那么如何打造围绕服务的核心竞争力&#xff1f;相信以下案例会给大家一些启发。 项目背景&#xff1a; 售后服务在市场竞争中的作用凸显 A公司是一家医疗器械生产制造企业…

LeetCode 热题 100 Day04

矩阵相关题型 Leetcode 73. 矩阵置零【中等】 题意理解&#xff1a; 将矩阵中0所在位置&#xff0c;行|列置换为全0 其中可以通过记录0元素所在的行、列号&#xff0c;来标记要置换的行|列 将对应位置置换为0 解题思路&#xff1a; 第一个思路&#xff1a; 可以…

02 VMware下载安装银河麒麟(Kylin)系统

02 VMware下载&安装银河麒麟&#xff08;Kylin&#xff09;系统 一、官网1、官网地址 二、下载1、官网下载&#xff08;1&#xff09;服务器操作系统&#xff08;2&#xff09;申请试用&#xff08;3&#xff09;产品试用申请&#xff08;4&#xff09;点击下载连接即可 2、…

单链表进阶题目,点进来看一下这些题你都会吗

c语言中的小小白-CSDN博客c语言中的小小白关注算法,c,c语言,贪心算法,链表,mysql,动态规划,后端,线性回归,数据结构,排序算法领域.https://blog.csdn.net/bhbcdxb123?spm1001.2014.3001.5343 给大家分享一句我很喜欢我话&#xff1a; 知不足而奋进&#xff0c;望远山而前行&am…

项目实践---贪吃蛇小游戏(下)

对于贪吃蛇小游戏&#xff0c;最主要的还是主函数部分&#xff0c;这里就和大家一一列举出来&#xff0c;上一章已经写过头文件了&#xff0c;这里就不多介绍了。 首先就是打印桌面&#xff0c;也就是背景&#xff0c;则对应的代码为&#xff1a; void SetPos(short x, short …

huggingface模型下载至本地并调用教程

huggingface内有许多预训练模型&#xff0c;可以在线调用模型或者将模型部署至本地&#xff0c;但有时候通过网址调用模型会很慢&#xff0c;有些服务器甚至无法通过网址调用… 那么&#xff0c;正题&#xff0c;如何将huggingface的模型部署至本地呢&#xff1f;其实很简单&am…

el-image组件预览图片同时操作页面

背景&#xff1a;el-image组件打开预览效果不能滑动页面。 Q:那么如何才能在打开遮罩层后还能操作页面呢&#xff1f; A:改变遮罩层的大小。CSS3有一个属性width&#xff1a;fit-content&#xff1b;可以解决这个问题。 打开F12看看饿了么的原生样式如下 加上width&#xff1…

R可视化:ggplot2绘制双y轴图

介绍 ggplot2绘制双y轴图加载R包 knitr::opts_chunk$set(message = FALSE, warning = FALSE) library(tidyverse) library(readxl)# rm(list = ls()) options(stringsAsFactors = F) options(future.globals.maxSize = 10000 * 1024^2)Importing data 下载Underdetection of c…

网页自动跳转到其他页面,点击浏览器返回箭头,回不到原来页面的问题

背景&#xff1a;今天产品提个需求&#xff0c;需要从index页面自动触发跳转到下一页面的事件&#xff0c;从而不做任何操作&#xff0c;直接跳转到test页面。 代码是这样的&#xff1a; index.vue: <template><div style"width:500px;height:600px;background-…

(三)Servlet教程——Tomcat安装与启动

首先打开浏览器在浏览器地址栏中输入清华大学开源软件镜像站地址&#xff0c;地址如下 https://mirrors.tuna.tsinghua.edu.cn/ 输入地址后回车会出现如下图所示的界面 在该界面找tomcat不是很好找&#xff0c;在搜索框中输入apache然后回车&#xff0c;输入apache后并回车后出…

WebSocket的原理、作用、API、常见注解和生命周期的简单介绍,附带SpringBoot示例

文章目录 原理作用客户端 API服务端 API生命周期常见注解SpringBoot示例 WebSocket是一种 通信协议 &#xff0c;它在 客户端和服务器之间建立了一个双向通信的网络连接 。WebSocket是一种基于TCP连接上进行 全双工通信 的 协议 。 WebSocket允许客户端和服务器在 单个TCP连接上…

AI道路交通违章智能抓拍系统解决方案

项目概述 背景 目前&#xff0c;XX市市全市民用汽车保有量94.62万辆&#xff0c;比上年末增长15.9%&#xff0c;其中私人汽车保有量35.48万辆&#xff0c;减少0.01%。轿车保有量39.45万辆&#xff0c;增长82.1%&#xff0c;其中私人轿车38.65万辆&#xff0c;增长82.1%。电动自…

【项目实战】基于高并发服务器的搜索引擎

【项目实战】基于高并发服务器的搜索引擎 目录 【项目实战】基于高并发服务器的搜索引擎搜索引擎部分代码index.htmlindex.hpplog.hppparser.cc&#xff08;用于对网页的html文件切分且存储索引关系&#xff09;searcher.hpputil.hpphttp_server.cc&#xff08;用于启动服务器和…

免费https证书申请

HTTPS证书&#xff0c;也称为SSL证书&#xff08;Secure Sockets Layer&#xff09;或TLS证书&#xff08;Transport Layer Security&#xff09;&#xff0c;是一种数字证书&#xff0c;用于在互联网通信中确保数据传输的安全性、完整性和真实性。它是基于公钥基础设施&#x…

VirtualFlow亮相核反应堆技术全国重点实验室2024学术年会

为加强先进核能技术领域科技创新与应用&#xff0c;核反应堆技术全国重点实验室及先进核能技术全国重点实验室2024年学术年会在四川成都启幕&#xff0c;9名院士和近百家科研院所、高校和企业等近700名专家学者齐聚一堂&#xff0c;聚焦和探讨核反应堆及先进核能重大基础理论和…
最新文章