Java uses FileChannel to copy files (improve copy efficiency)

FileChannel belongs to nio, and the bottom layer of FileChannel will use the zero copy of the operating system for optimization, which is more efficient than io.

Guide package

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

1. When the copied file is smaller than 2G

the code

    public void channelCopy(String sourcePath,String destPath){
    
    

        try {
    
    
            FileChannel sourceChannel = new FileInputStream(sourcePath).getChannel();
            FileChannel destChannel = new FileOutputStream(destPath).getChannel();

            sourceChannel.transferTo(0,sourceChannel.size(),destChannel);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }

    }

illustrate

sourcePath: source address, such as E:\xx\yy\a.txt
destPath: destination address, such as D:\yy\a.txt

sourceChannel.transferTo(0, sourceChannel.size(), destChannel);
parameter description
0: Indicates where to start copying from the source file.
sourceChannel.size(): How big is the copy.
destChannel: where to copy.
The return value of this method is the size of the actual copy. This method copies a maximum of 2G, and the part exceeding 2G will be discarded.

Resolve exceptions, and complete encapsulation of stream closures

public void channelCopy(String sourcePath,String destPath){
    
    
        FileChannel sourceChannel=null;
        FileChannel destChannel=null;

        try {
    
    
            sourceChannel = new FileInputStream(sourcePath).getChannel();
            destChannel = new FileOutputStream(destPath).getChannel();

            sourceChannel.transferTo(0,sourceChannel.size(),destChannel);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            try {
    
    
                if (sourceChannel!=null){
    
    
                    sourceChannel.close();
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }

            try {
    
    
                if (destChannel!=null){
    
    
                    destChannel.close();
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }

    }

2. The copied content is larger than 2G

 //超过 2g 大小的文件传输
    public void channelCopy(String sourcePath,String destPath){
    
    

        try {
    
    
            FileChannel sourceChannel = new FileInputStream(sourcePath).getChannel();
            FileChannel destChannel = new FileOutputStream(destPath).getChannel();
            //所要拷贝的原文件大小
            long size=sourceChannel.size();
            for (long left=size;left>0;){
    
    
                //transferSize所拷贝过去的真实长度
                //size - left计算出下次要拷贝的位置
                long transferSize = sourceChannel.transferTo((size - left), left, destChannel);
                //还剩余多少
                left=left-transferSize;
            }

        } catch (IOException e) {
    
    
            e.printStackTrace();
        }

    }

Resolve exceptions, and complete encapsulation of stream closures

//超过 2g 大小的文件传输
    public void channelCopy(String sourcePath,String destPath){
    
    

        FileChannel sourceChannel=null;
        FileChannel destChannel=null;
        try {
    
    
            sourceChannel = new FileInputStream(sourcePath).getChannel();
            destChannel = new FileOutputStream(destPath).getChannel();

            long size=sourceChannel.size();
            for (long left=size;left>0;){
    
    
                long transferSize = sourceChannel.transferTo((size - left), left, destChannel);
                left=left-transferSize;
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            try {
    
    
                if (sourceChannel!=null){
    
    
                    sourceChannel.close();
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }

            try {
    
    
                if (destChannel!=null){
    
    
                    destChannel.close();
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }

Guess you like

Origin blog.csdn.net/baiqi123456/article/details/128173791