java利用“映射文件访问”(MapperByteBuffer)处理文件与单纯利用Buffer来处理文件的快慢比较

  处理文件是java经常使用的操作,在对一个“大文件”(比如超过64M)进行操作时一点点速度的提高都会带来性能的巨大提升。然而我们经常使用的BufferxxStream,来直接处理大文件时,往往力不从心。

  java中“映射文件访问”机制则解决了这一问题,它把大文件的较小部分先放在内存里,将其余待读取的数据仍然放在硬盘里面。但是我们完全可以通过这样一个机制把这个大文件当作非常大的数组来使用。是不是很像操作系统里面对于应用的使用方法?没错,其实java正是利用了底层操作系统的文件映射工具来最大化提高性能。

  一般为了即能读又能写,我们使用RandomAccessFile来进行测试,同过该文件上的通道,然后调用FileChannel中的map()所产生的MappedByteBuffer,在map方法中有三个参数map(FileChannel.MapMode mode, long position, long size)。

  其中mode 来决定映射的文件访问的读/写机制,可以设置MapMode.READ_ONLY, MapMode.READ_WRITEMapMode.PRIVATE,通过名字可以看得出他们的作用,特别要注意MapMode.PRIVATE方法,如何你操作不当会导致你操作的文件无法被共享。

  而position为区域内的文件映射的位置开始,必须是非负数

   size为区域的大小是映射;必须是非负数,不大于Integer.MAX_VALUE

  其实position和size的设置就意味着我们映射某个大文件的较小的部分

  下面就来看我们使用了“映射文件访问”时,的性能提升把!

 

  以下是代码部分:

  1 package com.company;
  2 
  3 import java.io.*;
  4 import java.nio.IntBuffer;
  5 import java.nio.channels.FileChannel;
  6 
  7 /**
  8  * Created by chunmiao on 17-3-15.
  9  */
 10 public class MappedIOTest {
 11     //输入值的数量
 12     private static int numOfInts = 4000000;
 13     //测试读写值的数量大小
 14     private static int numOfUbuffInts = 200000;
 15 
 16     private abstract static class Tester{
 17         private String name;
 18         public Tester(String name){
 19             this.name = name;
 20         }
 21 
 22         //测试计时
 23         public void runTest(){
 24             System.out.println(name + ": ");
 25             try {
 26                 long start = System.nanoTime();
 27                 test();
 28                 long runtime = System.nanoTime() - start;
 29                 //输出test()运行时间,来判定操作时间大小
 30                 System.out.format("%.2f\n",runtime/1.0e9);
 31             }catch (IOException e){
 32                 throw new RuntimeException(e);
 33             }
 34         }
 35 
 36         public abstract void test() throws IOException;
 37     }
 38 
 39     private static Tester[] tests = {
 40             //直接写入数据
 41             new Tester("Stream Write") {
 42                 @Override
 43                 public void test() throws IOException {
 44                     DataOutputStream dos = new DataOutputStream(
 45                             new BufferedOutputStream(
 46                                     new FileOutputStream(new File("temp.tmp"))));
 47                     for (int i = 0; i < numOfInts; i ++){
 48                         dos.writeInt(i);
 49                     }
 50                     dos.close();
 51                 }
 52             },
 53             //使用MappedByteBuffer进行写文件操作
 54             new Tester("Mapped Write") {
 55                 @Override
 56                 public void test() throws IOException {
 57                     FileChannel fc = new RandomAccessFile("temp.tmp","rw").getChannel();
 58                     //fc.size()返回的是以byte为单位的数量值。
 59                     IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE,0,fc.size()).asIntBuffer();
 60                     for(int i =0 ; i < numOfInts ; i ++){
 61                         try {
 62                             ib.put(i);
 63                         }catch (Exception e){
 64                             System.out.println(i);
 65                         }
 66                     }
 67                     fc.close();
 68                 }
 69             },
 70             //直接读数据
 71             new Tester("Stream Read") {
 72                 @Override
 73                 public void test() throws IOException {
 74                     DataInputStream dis = new DataInputStream(
 75                             new BufferedInputStream(new FileInputStream("temp.tmp")));
 76                     for (int i = 0 ; i < numOfInts ; i ++){
 77                         dis.readInt();
 78                     }
 79                     dis.close();
 80                 }
 81             },
 82             //使用MappedByteBuffer进行读文件操作
 83             new Tester("Mapped Read") {
 84                 @Override
 85                 public void test() throws IOException {
 86                     FileChannel fc = new FileInputStream(new File("temp.tmp")).getChannel();
 87                     IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY,0,fc.size()).asIntBuffer();
 88                     while (ib.hasRemaining()){
 89                         ib.get();
 90                     }
 91                     fc.close();
 92                 }
 93             },
 94             //直接进行读/写操作
 95             new Tester("Stream Read/Write") {
 96                 @Override
 97                 public void test() throws IOException {
 98                     RandomAccessFile raf = new RandomAccessFile(new File("temp.tmp"),"rw");
 99                     //对数据进行进行错位,以便同时进行读写操作
100                     raf.writeInt(1);
101                     for (int i = 0 ; i < numOfUbuffInts ; i ++){
102                         //之所以减4的原因是raf.length()返回以byte为单位的空间大小,而1 (int) = 4 (byte),因此需要减4
103                         raf.seek(raf.length() - 4);
104                         raf.writeInt(raf.readInt());
105                     }
106                     raf.close();
107                 }
108             },
109             //使用MappedByteBuffer进行读/写操作
110             new Tester("Mapped Read/Write") {
111                 @Override
112                 public void test() throws IOException {
113                     FileChannel fc = new RandomAccessFile(new File("temp.tmp"),"rw").getChannel();
114                     IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE,0,fc.size()).asIntBuffer();
115                     ib.put(0);
116                     for (int i = 1 ; i < numOfUbuffInts ; i ++){
117                         //对数据进行进行错位,以便同时进行读写操作
118                         ib.put(ib.get(i - 1));
119                     }
120                     fc.close();
121                 }
122             }
123     };
124 
125     public static void main(String[] args) {
126         //进行测试
127         for (Tester tester : tests){
128             tester.runTest();
129         }
130     }
131 }

以下是运行结果:

 

   

 

posted on 2017-03-15 22:19  zangcunmiao  阅读(1736)  评论(0编辑  收藏  举报

导航