Java usa FileChannel para copiar archivos (mejorar la eficiencia de copia)

FileChannel pertenece a nio, y la capa inferior de FileChannel utilizará la copia cero del sistema operativo para la optimización, que es más eficiente que io.

Paquete de guía

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

1. Cuando el archivo copiado es más pequeño que 2G

el código

    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();
        }

    }

ilustrar

sourcePath: dirección de origen, como E:\xx\yy\a.txt
destPath: dirección de destino, como D:\yy\a.txt

sourceChannel.transferTo(0, sourceChannel.size(), destChannel);
descripción del parámetro
0: indica dónde empezar a copiar desde el archivo de origen.
sourceChannel.size(): Qué tan grande es la copia.
destChannel: donde copiar.
El valor de retorno de este método es el tamaño de la copia real. Este método copia un máximo de 2G, y la parte que supere los 2G será descartada.

Resuelva excepciones y complete la encapsulación de cierres de flujo

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. El contenido copiado es más grande que 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();
        }

    }

Resuelva excepciones y complete la encapsulación de cierres de flujo

//超过 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();
            }
        }
    }

Supongo que te gusta

Origin blog.csdn.net/baiqi123456/article/details/128173791
Recomendado
Clasificación