使用MappedByteBuffer(映射文件)对excel表格进行随机定位和读取

来源:互联网 发布:淘宝上卖高仿鞋 编辑:程序博客网 时间:2024/06/06 09:19
   首先我个人对映射文件并不是很熟悉,之前可以说从来没有接触过,只有不断的去学习,从随机文件到映射文件,完全是考虑到读取速度上的问题,之所以选择映射文件,是因为它的速度比随机文件快很多,下面是一个速度对比的列子。           //随机文件           int length=1*1024*1024;           try{           long start1 = System.currentTimeMillis();  //开始时间           System.out.println("开始时间1:"+start1);            //新建一个txt文件           RandomAccessFile file1 = new RandomAccessFile("D://work1.txt", "rw");             //使用随机文件进行1Mb数据的存贮以及取出           for (int i = 0; i <length; i++) {                file1.write('a');           }           file1.seek(0);   //将指针移到第一个           byte aa[]=new byte[(int) file1.length()];           file1.read(aa);           file1.close();             System.out.println("结束时间"+System.currentTimeMillis());            System.out.println("随机文件: "+(double)(System.currentTimeMillis()-start1) / 1000 + "s");                   }catch(Exception e){             e.printStackTrace();         }  

开始时间1:1495597349540
结束时间1495597360921
随机文件: 11.381s

    //映射文件    int length=1*1024*1024;try{      long start1 = System.currentTimeMillis();  //开始时间    System.out.println("开始时间1:"+start1);    RandomAccessFile file2 = new RandomAccessFile("D://work2.txt", "rw");   //新建一个txt文件    FileChannel fco = file2.getChannel();      MappedByteBuffer mbbo = fco.map(FileChannel.MapMode.READ_WRITE, 0, length);      //使用映射文件对数据的存取以及取出   for (int i = 0; i < length; i++) {         mbbo.put((byte) 'a');      }      for (int i = 0; i < length; i++) {           mbbo.get(i);         }      file2.close();     fco.close();      System.out.println("结束时间"+System.currentTimeMillis());          System.out.println("映射文件: "+(double)(System.currentTimeMillis()-start1) / 1000 + "s");            }catch(Exception e){      e.printStackTrace();  } 

开始时间1:1495597841321
结束时间1495597841368
映射文件: 0.047s

不用想,这个速度简直了,甚至不及一秒钟就能完成1M数据的写入和取出,本着这个原因,使用映射文件对表格进行定位和处理。
RandomAccessFile file=new RandomAccessFile(“D://TDTest.csv”, “rw”);
RandomAccessFile file1=new RandomAccessFile(“D://TDTest1.csv”, “rw”);
FileChannel channel=file1.getChannel();
MappedByteBuffer mapBuffer=channel.map(FileChannel.MapMode.READ_WRITE, 0, file.length());
long start1 = System.currentTimeMillis(); //开始时间
System.out.println(“开始时间1:”+start1);
String FirstLine=file.readLine();
int first=file.readLine().getBytes().length+1;
System.out.println(“第一行的长度:”+first);

String TwoLine=file.readLine();int two=file.readLine().getBytes().length+1;System.out.println("第二行的长度:"+two);        String []lines;    String str="";    int a=3;            while( (str=file.readLine()) != null ) {        mapBuffer.clear();        lines=str.split(",");        for (int i = 1; i < lines.length; i++) {   //循环,把每一行第一个数排除在外,由于第一个数是时间            float temp = Float.valueOf(lines[i]);  //将数组的每一个值转化为float值            mapBuffer.putFloat(temp);    //将拿到的float存贮进入mappedbytebuffer         }        System.out.println("拿到第"+a+"行的float的值:");         for (int i = 0; i < (lines.length-1)*4; i+=4) {            mapBuffer.getFloat(i);   //拿到存在文件中的float数            System.out.print(mapBuffer.getFloat(i)+" ");        }        System.out.println("");        a++;    }            System.out.println("结束时间"+System.currentTimeMillis());     System.out.println("Spend1: "+(double)(System.currentTimeMillis()-start1) / 1000 + "s");        file.close();file1.close();

以下是粘贴出本次使用的excel表格的几行数据
temperature test results
time TA-1 TA-2 TA-3 TA-4 TA-5 TA-6 TA-7 TA-8 TA-V TA-B TB-1 TB-2 TB-3 TB-4 TB-5 TB-6 TA-7 TA-8 TB-V TB-B
9467263980 18.2 17.1 17.6 18.2 15.1 17.7 17.7 17.8 23.3 23.5 23.3 30.3 23.1 23.1 23.1 23.1 0 0 0 0
9467263985 18.2 17.1 17.7 18.2 15.1 17.7 17.7 17.8 23.5 23.5 23.3 30.3 23.1 23.1 23.1 23.1 0 0 0 0
9467263990 18.2 17.2 17.6 18.2 15.1 17.7 17.7 17.8 23.5 23.3 23.3 30.3 23.1 23.1 23.2 23.1 0 0 0 0
9467263995 18.2 17.2 17.6 18.2 15.1 17.7 17.8 17.8 23.3 23.5 23.3 30.3 23.1 23.1 23.1 23.1 0 0 0 0
9467264000 18.2 17.1 17.5 18.2 15.3 17.7 17.7 17.8 23.3 23.3 23.3 30.3 23.1 23.1 23.1 23.1 0 0 0 0
9467264005 18.2 17.1 17.6 18.2 15.3 17.7 17.7 17.7 23.3 23.5 23.2 30.3 23.1 23.2 23.1 23.1 0 0 0 0
9467264010 18.2 17.1 17.6 18.1 15.3 17.7 17.7 17.8 23.3 23.5 23.2 30.3 23.1 23.1 23.1 23.1 0 0 0 0
9467264015 18.2 17.1 17.6 18.2 15.3 17.7 17.7 17.8 23.3 23.5 23.2 30.3 23.1 23.1 23.1 23.1 0 0 0 0

这里是运行结果,由于这个表格的数据比较多,所以就粘贴了几行运行结果显示:
拿到第6657行的float的值:
9.4672968E9 16.2 16.0 16.1 19.5 15.8 16.5 16.5 21.2 22.8 22.7 22.7 27.6 22.5 22.6 22.5 24.0 0.0 0.0 0.0
拿到第6658行的float的值:
9.4672968E9 16.3 16.0 16.1 19.7 15.6 16.5 16.6 21.7 22.8 22.7 22.7 27.7 22.5 22.6 22.5 23.8 0.0 0.0 0.0
拿到第6659行的float的值:
9.4672968E9 16.2 16.0 16.1 19.7 15.6 16.5 16.5 21.3 22.8 22.7 22.7 27.7 22.5 22.6 22.5 23.7 0.0 0.0 0.0
拿到第6660行的float的值:
9.4672968E9 16.2 16.0 16.1 20.2 15.8 16.5 16.3 21.5 22.8 22.7 22.7 27.6 22.5 22.6 22.5 24.0 0.0 0.0 0.0
拿到第6661行的float的值:
9.4672978E9 16.3 15.8 16.1 20.0 15.6 16.5 16.5 21.8 22.8 22.7 22.7 27.7 22.5 22.5 22.5 23.7 0.0 0.0 0.0
拿到第6662行的float的值:
9.4672978E9 16.2 16.0 16.1 20.0 15.6 16.5 16.5 21.3 22.8 22.7 22.7 28.2 22.5 22.5 22.5 23.2 0.0 0.0 0.0
拿到第6663行的float的值:
9.4672978E9 16.2 15.8 16.0 20.1 15.6 16.5 16.5 21.5 22.8 22.7 22.7 27.7 22.5 22.5 22.5 23.7 0.0 0.0 0.0
拿到第6664行的float的值:
9.4672978E9 16.2 15.8 16.1 20.1 15.6 16.5 16.5 22.2 22.8 22.7 22.7 27.7 22.3 22.5 22.5 23.8 0.0 0.0 0.0
拿到第6665行的float的值:
9.4672978E9 16.2 15.8 16.1 19.8 15.6 16.5 16.5 20.5 22.8 22.7 22.7 27.7 22.5 22.5 22.5 23.7 0.0 0.0 0.0

这里简单的讲了一下MappedByteBuffer读取excel表格的使用,希望能帮助到大家,第一次发博文,谢谢!

阅读全文
0 0
原创粉丝点击