不同的方式文件写入性能对比

  • Post author:
  • Post category:其他


​
package com.test;

import org.junit.Test;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class mytest {
    String TEST_PATH = "e:\\myfile\\";

    //1、单字节复制文件
    @Test
    public void perByteOperation() {
        System.out.println("开始单字节单字节复制文件...");
        long start = System.currentTimeMillis();
        try (FileInputStream fis = new FileInputStream(TEST_PATH + "dockerfile.vep");
             FileOutputStream fos = new FileOutputStream(TEST_PATH + "ts1.vep")) {
            int i;
            while ((i = fis.read()) != -1) {
                fos.write(i);
            }
        } catch (Exception e) {

            System.out.println("单字节单字节复制文件异常" + e);
        }
        Long time = System.currentTimeMillis() - start;
        System.out.println("结束单字节单字节复制文件...,耗时:" + time + "ms");
//          开始单字节单字节复制文件...
//          结束单字节单字节复制文件...,耗时:136896ms
    }

    //2、1000字节缓冲区复制文件
/*    @Test
    public void bufferOperationWith1000() {
        System.out.println("开始带1000字节缓冲区复制文件...");
        long start = System.currentTimeMillis();
        try (FileInputStream fis = new FileInputStream(TEST_PATH + "dockerfile.vep");
             FileOutputStream fos = new FileOutputStream(TEST_PATH + "ts2.vep")) {
            byte[] buf = new byte[1000];
            int len;
            while ((len = fis.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
        } catch (Exception e) {
            System.out.println("带1000字节缓冲区复制文件"+ e);

        }

        Long time = System.currentTimeMillis() - start;
        System.out.println("结束带1000字节缓冲区复制文件...,耗时:"+ time +"ms");
//      开始带1000字节缓冲区复制文件...
//      结束带1000字节缓冲区复制文件...,耗时:261ms
    }*/

    //3、Buffered缓冲区复制文件
/*    @Test
    public void bufferStreamByteOperation() {
        System.out.println("开始带缓冲区复制文件...");
        long start = System.currentTimeMillis();
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(TEST_PATH + "dockerfile.vep"));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(TEST_PATH + "ts3.txt"))) {
            int i;
            while ((i = bis.read()) != -1) {
                bos.write(i);
            }
        } catch (Exception e) {
            System.out.println("带缓冲区复制文件文件异常" + e);
        }
        Long time = System.currentTimeMillis() - start;
        System.out.println("结束带缓冲区复制文件...,耗时:"+ time +"ms");
//        开始带缓冲区复制文件...
//        结束带缓冲区复制文件...,耗时:451ms
    }*/

    //4、在BufferedStream基础上,写入的时候新增一个8KB的缓冲
/*    @Test
    public void bufferStreamBufferOperation() {
        System.out.println("bufferStreamBufferOperation开始带缓冲区复制文件...");
        long start = System.currentTimeMillis();
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(TEST_PATH + "dockerfile.vep"));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(TEST_PATH + "ts4.vep"))) {
            byte[] buf = new byte[8196];
            int i;
            while ((i = bis.read(buf)) != -1) {
                bos.write(buf, 0, i);
            }
        } catch (Exception e) {
            System.out.println("带缓冲区复制文件文件异常" + e);
        }
        Long time = System.currentTimeMillis() - start;
        System.out.println("结束bufferStream带8KB缓冲区复制文件...,耗时:"+ time +"ms");
//        bufferStreamBufferOperation开始带缓冲区复制文件...
//        结束bufferStream带8KB缓冲区复制文件...,耗时:94ms
    }*/

    //5、不采用缓冲流,只新增8KB的缓冲数组

/*    @Test
    public void streamBufferOperation() {
        System.out.println("streamBufferOperation开始只新增8KB的缓冲数组复制文件...");
        long start = System.currentTimeMillis();
        try (FileInputStream fis = new FileInputStream(TEST_PATH + "dockerfile.vep");
             FileOutputStream fos = new FileOutputStream(TEST_PATH + "ts5.vep")) {
            byte[] buf = new byte[8196];
            int i;
            while ((i = fis.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
        } catch (Exception e) {
            System.out.println("带缓冲区复制文件文件异常"+ e);
        }
        Long time = System.currentTimeMillis() - start;
        System.out.println("结束只新增8KB的缓冲数组复制文件...,耗时:"+ time +"ms");
//        streamBufferOperation开始只新增8KB的缓冲数组复制文件...
//        结束只新增8KB的缓冲数组复制文件...,耗时:109ms
    }*/

// 6、DMA(直接内存访问),数据从磁盘经过总线直接发送到目标文件,无需经过内存和 CPU 进行数据中转,这种方式更快。

/*    @Test
    public void fileChannelOperation() throws IOException {
        System.out.println("fileChannelOperation开始复制文件...");
        long start = System.currentTimeMillis();
        FileChannel in = FileChannel.open(Paths.get(TEST_PATH + "dockerfile.vep"), StandardOpenOption.READ);
        FileChannel out = FileChannel.open(Paths.get(TEST_PATH + "ts6.vep"), StandardOpenOption.CREATE,
                StandardOpenOption.WRITE);
        in.transferTo(0, in.size(), out);
        Long time = System.currentTimeMillis() - start;
        System.out.println("结束fileChannelOperation复制文件...,耗时:"+ time +"ms");
//      fileChannelOperation开始复制文件...
//      结束fileChannelOperation复制文件...,耗时:42ms
    }*/


}

​

void write(int b) 将指定的字节写入此文件输出流 一次写一个字节数据

void write(byte[] b)将 b.length字节从指定的字节数组写入此文件输出流 一次写一个字节数组数据

void write(byte[] b, int off, int len)将 len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流一次写一个字节数组的部分数据



版权声明:本文为San6653原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。