映射文件读写性能比较

下面代码案例分别实现了用流来读写文件和使用文件映射读写文件文件的时间,根据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;i<numOfInts;i++){
                    os.writeInt(i);
                }
                os.close();
            }},
            new 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;i<numOfBuffInts;i++){
                   intBuffer.put(i);
               }
               fileChannel.close();
           }},
            new 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;i<file.length()/4;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;i<numOfBuffInts;i++){
                intBuffer.put(intBuffer.get(i-1));
            }
            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;i<numOfBuffInts;i++){
                        randomAccessFile.seek(randomAccessFile.length()-4);
                        randomAccessFile.writeInt(randomAccessFile.readInt());
                    }
                    randomAccessFile.close();
                }
            }
    };

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

运行的结果如下图
运行结果显示

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());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值