映射文件读写性能比较

下面代码案例分别实现了用流来读写文件和使用文件映射读写文件文件的时间,根据thinking in java上面的案例实现

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

/**
 * All rights reserved.
 * Created by zhaideyin on 2017/10/18.
 * Description:
 */
public class MappIO {
    private static int numOfInts=4000000;
    private static int numOfBuffInts=2000;
    //使用模板模式
    private abstract static  class Tester{
        private String name;
        public Tester(String name){
            this.name=name;
        }
        //定义了一个模板方法
        public void runTest() throws IOException {
            System.out.print(name+":");
            long start=System.nanoTime();
            //不同的子类实现test()的具体实现不同
             test();
             double duration= System.nanoTime()-start;
            System.out.format("%.2f",duration/1.0e9);
            System.out.println();
        }
        public abstract  void test() throws IOException;
    }
    private static Tester[] testers={
            new Tester("stream write") {
            @Override
            public void test() throws IOException {
                DataOutputStream os=new DataOutputStream(new FileOutputStream(new File("C:\\test.txt")));
                for(int i=0;inew Tester("mapped write") {
           @Override
            public void test() throws IOException {
               FileChannel fileChannel=new RandomAccessFile("C:\\test.txt","rw").getChannel();
               IntBuffer intBuffer=fileChannel.map(FileChannel.MapMode.READ_WRITE,0,fileChannel.size()).asIntBuffer();
               for(int i=0;inew Tester("Stream read") {
                @Override
                public void test() throws IOException {
                     File file=new File("C:\\test.txt");
                    DataInputStream dataInputStream=new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
                    for(int i=0;i4;i++){
                        dataInputStream.readInt();
                    }
                    dataInputStream.close();
                }
            },
            new Tester("mapped read") {
                @Override
                public void test() throws IOException {
                    FileChannel fileChannel=new RandomAccessFile("C:\\test.txt","rw").getChannel();
                    IntBuffer intBuffer=fileChannel.map(FileChannel.MapMode.READ_ONLY,0,fileChannel.size()).asIntBuffer();
                   while (intBuffer.hasRemaining()){
                       intBuffer.get();
                   }
                    fileChannel.close();

                }
            },
            new Tester("mapped  write read") {
        @Override
        public void test() throws IOException {
            FileChannel fileChannel=new RandomAccessFile("C:\\test.txt","rw").getChannel();
            IntBuffer intBuffer=fileChannel.map(FileChannel.MapMode.READ_WRITE,0,fileChannel.size()).asIntBuffer();
            intBuffer.put(0);
            for(int i=1;i1));
            }
            fileChannel.close();

        }
    },
            new Tester("stream  write read") {
                @Override
                public void test() throws IOException {
                    RandomAccessFile randomAccessFile=new RandomAccessFile(new File("C:\\test.txt"),"rw");
                    randomAccessFile.writeInt(1);
                    for(int i=0;i4);
                        randomAccessFile.writeInt(randomAccessFile.readInt());
                    }
                    randomAccessFile.close();
                }
            }
    };

    public static void main(String[] args) throws IOException {
        for(Tester tester:testers){
            tester.runTest();
        }
    }
}

运行的结果如下图
映射文件读写性能比较_第1张图片

java nio的整体框架

java nio其实就是java new io的简称,在新的io设计中,使用了不同于老的io架构,java新的io主要由两个部分组成,channel和byteBuffer,byebuffer是和channel交互的唯一方式,我们可以在通道中读写数据,每一个通道都被Selector所管理,新的iO在读写性能上面确实要快很多,一般的读写方式如下:

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

/**
 * All rights reserved.
 * Created by zhaideyin on 2017/10/19.
 * Description:
 */
public class NIO {
    private static final int SIZE=1024;
    public static void main(String[] args) throws IOException {
        //write data
        FileChannel fileChannel=new FileOutputStream("c:\\test.txt").getChannel();
        fileChannel.write(ByteBuffer.wrap("hello world!!".getBytes()));
        fileChannel.close();
        //append to end
        fileChannel=new RandomAccessFile("c:\\test.txt","rw").getChannel();
        fileChannel.position(fileChannel.size());
        fileChannel.write(ByteBuffer.wrap("my dear!".getBytes()));
        fileChannel.close();
        //read data
        fileChannel=new FileInputStream("c:\\test.txt").getChannel();
        ByteBuffer byteBuffer=ByteBuffer.allocate(SIZE);
        fileChannel.read(byteBuffer);
        byteBuffer.flip();
        while (byteBuffer.hasRemaining()){
            System.out.print((char) byteBuffer.get());
        }
    }
}

你可能感兴趣的:(映射文件读写性能比较)