io与各种nio的效率测试

217 阅读1分钟
public static Integer size=10240;
   public static void main(String[] args) throws IOException {

    File file=new File("D:\\jar\\PCQQ2020.exe");
    String filepath="D:\\jar\\test\\PCQQ2020.exe";
    Long startTime=System.currentTimeMillis();
    channelReadAndWrite(file,filepath);
    Long endTime=System.currentTimeMillis();
    System.out.println("nio耗时:"+(endTime-startTime));
    filepath="D:\\jar\\test\\PCQQ2021.exe";
    Long startTime1=System.currentTimeMillis();
    copyReadAndWrite(file,filepath);
    Long endTime1=System.currentTimeMillis();
    System.out.println("io耗时:"+(endTime1-startTime1));
    filepath="D:\\jar\\test\\PCQQ2022.exe";
    nioChannel1(file,filepath);
    filepath="D:\\jar\\test\\PCQQ2023.exe";
    nioChannel(file,filepath);




}
public static void channelReadAndWrite(File file, String filepath)throws IOException{
    FileInputStream fs=new FileInputStream(file);
    FileOutputStream os=new FileOutputStream(filepath);
    FileChannel readChannel = fs.getChannel();
    FileChannel writeChannel= os.getChannel();
    ByteBuffer buffer=ByteBuffer.allocate(size);
    while (readChannel.read(buffer)!=-1){
        buffer.flip();
        writeChannel.write(buffer);
        buffer.clear();
    }
    readChannel.close();
    writeChannel.close();
    os.close();
    fs.close();
}
public static void nioChannel(File file, String filepath){
    long startTime = System.currentTimeMillis();

    FileChannel inChannel = null;
    FileChannel outChannel = null;

    try {
        // 获取通道
        inChannel = FileChannel.open(Paths.get(file.getAbsolutePath()), StandardOpenOption.READ);
        outChannel = FileChannel.open(Paths.get(filepath), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);

        // 设置内存映射文件
        MappedByteBuffer imb = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        MappedByteBuffer omb = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());

        // 直接对缓冲区进行数据的读写操作
        byte[] data = new byte[imb.limit()];
        imb.get(data);
        omb.put(data);
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        if(inChannel != null){
            try {
                inChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(outChannel != null){
            try {
                outChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 记录结束时间
    long endTime = System.currentTimeMillis();
    System.out.println("使用直接缓冲区完成文件的复制(内存映射文件)耗时为" + (endTime - startTime) + "毫秒");
}
public static void nioChannel1(File file, String filepath){


        long startTime = System.currentTimeMillis();

        FileChannel inChannel = null;
        FileChannel outChannel = null;

        try {
            inChannel = FileChannel.open(Paths.get(file.getAbsolutePath()), StandardOpenOption.READ);
            outChannel = FileChannel.open(Paths.get(filepath), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(inChannel != null){
                try {
                    inChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outChannel != null){
                try {
                    outChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        long endTime = System.currentTimeMillis();
        System.out.println("通道之间的数据传输(直接缓存区)耗时为" + (endTime - startTime) + "毫秒");

}
public static void copyReadAndWrite(File file,String filePath) throws IOException {
    FileInputStream fs=new FileInputStream(file);
    FileOutputStream os=new FileOutputStream(filePath);
    byte[] bytes=new byte[size];
    int len=0;
    while((len=fs.read(bytes))!=-1){
        os.write(bytes,0,len);
    }
    os.close();
    fs.close();

}

总结io的时间比较稳,nio的时间上下波动比较大,size改大点,io的总体效率较高