mysterin / question_and_answer

1 stars 0 forks source link

各种复制文件方法 #154

Closed mysterin closed 5 years ago

mysterin commented 5 years ago
package com.linxb;

import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioTest {

    @Test
    public void test() throws IOException {
        String src = "/home/linxb/sslkeylog.log1"; // 5M 大小文件
        String dst = "/home/linxb/sslkeylog.log2";
        long start = System.currentTimeMillis();

        // 15175ms
        // copyFileBybyte(src, dst);

        // 42ms
        // copyFileByBytes(src, dst);

        // 405ms
        // copyFileByBuffer(src, dst);

        // 23ms
        // copyFileByBuffers(src, dst);

        // 101ms
        // copyFileByChannel(src, dst);

        // 17ms
        copyFileByChannelTrans(src, dst);
        long end = System.currentTimeMillis();
        System.out.println("time: " + (end - start) + "ms");
    }

    public void copyFileBybyte(String src, String dst) throws IOException {
        FileInputStream fis = new FileInputStream(new File(src));
        FileOutputStream fos = new FileOutputStream(new File(dst));

        int b;
        while ((b = fis.read()) != -1) {
            fos.write(b);
        }
        fis.close();
        fos.close();
    }

    public void copyFileByBytes(String src, String dst) throws IOException {
        FileInputStream fis = new FileInputStream(new File(src));
        FileOutputStream fos = new FileOutputStream(new File(dst));

        byte[] bytes = new byte[1024];
        int len;
        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }
    }

    public void copyFileByBuffer(String src, String dst) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(src)));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dst)));
        int b;
        while ((b = bis.read()) != -1) {
            bos.write(b);
        }
        bis.close();
        bos.close();
    }

    public void copyFileByBuffers(String src, String dst) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(src)));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dst)));
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        bis.close();
        bos.close();
    }

    public void copyFileByChannel(String src, String dst) throws IOException {
        // FileInputStream fis = new FileInputStream(new File(src));
        // FileChannel fic = fis.getChannel();
        // FileOutputStream fos = new FileOutputStream(new File(dst));
        // FileChannel foc = fos.getChannel();

        RandomAccessFile raf = new RandomAccessFile(src, "r");
        FileChannel fic = raf.getChannel();
        RandomAccessFile of = new RandomAccessFile(dst, "rw");
        FileChannel foc = of.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        while ((fic.read(byteBuffer)) != -1) {
            byteBuffer.flip();
            foc.write(byteBuffer);
            // 注意这里一定要调用 clear() 方法清理
            // 不然会一直死循环
            byteBuffer.clear();
        }
        // fic.close();
        // foc.close();
        raf.close();
        of.close();
    }

    public void copyFileByChannelTrans(String src, String dst) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(src, "r");
        FileChannel fic = raf.getChannel();
        RandomAccessFile of = new RandomAccessFile(dst, "rw");
        FileChannel foc = of.getChannel();
        foc.transferFrom(fic, 0, fic.size());
    }
}