<code id="qf3hh"></code>
  • <menuitem id="qf3hh"></menuitem>
  • <strike id="qf3hh"><label id="qf3hh"></label></strike>

  • ?
      開發(fā)技術(shù) / Technology

      java解決大數(shù)據(jù)讀寫問題

      日期:2015年5月25日  作者:zhjw  來源:互聯(lián)網(wǎng)    點擊:1262

      把SRTM的數(shù)據(jù)讀寫了一下,可以用。

       

      JAVA內(nèi)存映射文件:http://jiangzhengjun.iteye.com/blog/515745

       

      內(nèi)存映射文件能讓你創(chuàng)建和修改那些因為太大而無法放入內(nèi)存的文件。有了內(nèi)存映射文件,你就可以認(rèn)為文件已經(jīng)全部讀進了內(nèi)存,然后把它當(dāng)成一個非常大的數(shù)組來訪問。這種解決辦法能大大簡化修改文件的代碼。
      fileChannel.map(FileChannel.MapMode mode, long position, long size)將此通道的文件區(qū)域直接映射到內(nèi)存中。注意,你必須指明,它是從文件的哪個位置開始映射的,映射的范圍又有多大;也就是說,它還可以映射一個大文件的某個小片斷。


      MappedByteBuffer是ByteBuffer的子類,因此它具備了ByteBuffer的所有方法,但新添了force()將緩沖區(qū)的內(nèi)容強制刷新到存儲設(shè)備中去、load()將存儲設(shè)備中的數(shù)據(jù)加載到內(nèi)存中、isLoaded()位置內(nèi)存中的數(shù)據(jù)是否與存儲設(shè)置上同步。這里只簡單地演示了一下put()和get()方法,除此之外,你還可以使用asCharBuffer( )之類的方法得到相應(yīng)基本類型數(shù)據(jù)的緩沖視圖后,可以方便的讀寫基本類型數(shù)據(jù)。

      Java代碼 復(fù)制代碼 收藏代碼
      1. import java.io.RandomAccessFile;   
      2. import java.nio.MappedByteBuffer;   
      3. import java.nio.channels.FileChannel;   
      4.   
      5. public class LargeMappedFiles {   
      6.     static int length = 0x8000000// 128 Mb   
      7.   
      8.     public static void main(String[] args) throws Exception {   
      9.         // 為了以可讀可寫的方式打開文件,這里使用RandomAccessFile來創(chuàng)建文件。   
      10.         FileChannel fc = new RandomAccessFile("test.dat""rw").getChannel();   
      11.         //注意,文件通道的可讀可寫要建立在文件流本身可讀寫的基礎(chǔ)之上   
      12.         MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, length);   
      13.         //寫128M的內(nèi)容   
      14.         for (int i = 0; i < length; i++) {   
      15.             out.put((byte'x');   
      16.         }   
      17.         System.out.println("Finished writing");   
      18.         //讀取文件中間6個字節(jié)內(nèi)容   
      19.         for (int i = length / 2; i < length / 2 + 6; i++) {   
      20.             System.out.print((char) out.get(i));   
      21.         }   
      22.         fc.close();   
      23.     }   
      24. }  
      [java] view plaincopy
       
      1. import java.io.RandomAccessFile;  
      2. import java.nio.MappedByteBuffer;  
      3. import java.nio.channels.FileChannel;  
      4.   
      5. public class LargeMappedFiles {  
      6.     static int length = 0x8000000// 128 Mb  
      7.   
      8.     public static void main(String[] args) throws Exception {  
      9.         // 為了以可讀可寫的方式打開文件,這里使用RandomAccessFile來創(chuàng)建文件。  
      10.         FileChannel fc = new RandomAccessFile("test.dat""rw").getChannel();  
      11.         //注意,文件通道的可讀可寫要建立在文件流本身可讀寫的基礎(chǔ)之上  
      12.         MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, length);  
      13.         //寫128M的內(nèi)容  
      14.         for (int i = 0; i < length; i++) {  
      15.             out.put((byte'x');  
      16.         }  
      17.         System.out.println("Finished writing");  
      18.         //讀取文件中間6個字節(jié)內(nèi)容  
      19.         for (int i = length / 2; i < length / 2 + 6; i++) {  
      20.             System.out.print((char) out.get(i));  
      21.         }  
      22.         fc.close();  
      23.     }  
      24. }  

       

      盡管映射寫似乎要用到FileOutputStream,但是映射文件中的所有輸出 必須使用RandomAccessFile,但如果只需要讀時可以使用FileInputStream,寫映射文件時一定要使用隨機訪問文件,可能寫時要讀的原因吧。

       

      該程序創(chuàng)建了一個128Mb的文件,如果一次性讀到內(nèi)存可能導(dǎo)致內(nèi)存溢出,但這里訪問好像只是一瞬間的事,這是因為,真正調(diào)入內(nèi)存的只是其中的一小部分,其余部分則被放在交換文件上。這樣你就可以很方便地修改超大型的文件了(最大可以到2 GB)。注意,Java是調(diào)用操作系統(tǒng)的"文件映射機制"來提升性能的。

       

       

      package cn.edu.xjtu.nhpcc.jenva.file;
      import   java.io.*;   
      import   java.nio.*;   
      import   java.nio.channels.*;

      public class LargeFileReader {


         static   int   length   =   0x8FFFFFF;   //   128   Mb 
         
       /**
        * @param args
        * @throws IOException 
        * @throws FileNotFoundException 
        */
       public static void main(String[] args) throws FileNotFoundException, IOException {
       
        /* The following works well.
           MappedByteBuffer out =  new RandomAccessFile( "test.dat",   "rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);   
           for(int   i   =   0;   i   <   length;   i++)   
           out.put((byte) 'x');   
           System.out.println( "Finished   writing ");   
           for(int   i   =   length/2;   i   <   length/2   +   6;   i++)   
                   System.out.print((char)out.get(i));       //read   file   
          */
        
        /* The following is ours. */
        FileChannel fc = new RandomAccessFile( "srtm1","r").getChannel();
        System.out.println(fc.size());
        long maxValue = length;
        if (maxValue>fc.size()) {
         maxValue = fc.size();
        }
        
           MappedByteBuffer out =  fc.map(FileChannel.MapMode.READ_ONLY, 0, maxValue);   
           
           FileWriter fw = new FileWriter("srtm1-2");
           int line =3601;
           
           
           for(int   i   =   0;   i   <   maxValue;   i++) {
            if (i != 0 &&i %(maxValue/line) == 0 ) {
             
             fw.append("/n");
             fw.append((char)out.get(i));
            }else {
             if (i<maxValue){
              //System.out.print((char)out.get(i));
              fw.append((char)out.get(i));
             }
            }
           } 
           fw.close();
      }
      }

       

       

       

      package cn.edu.xjtu.nhpcc.jenva.file;

      import java.io.BufferedReader;
      import java.io.FileReader;
      import java.io.IOException;
      import java.util.ArrayList;

      public class FileReaderTest {

       /**
        * @param args
        * @throws IOException 
        */
       public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        /*
        MyFileReader f = new MyFileReader();
        f.reader("hi");
        */
        
        /*
         * 下面這段代碼,讀出來的長度連空格也算。
         */
        /*
        BufferedReader is = new BufferedReader(new FileReader("dbPoolParameter"));
        ArrayList<Float> a = new ArrayList<Float>();
         
         String s = is.readLine();
         System.out.println(s.length());
         */
        
        /*
         * 下面這段代碼,讀出來的是純數(shù)字,空格不計入。
         */
         /*
        BufferedReader is = new BufferedReader(new FileReader("srtm1"));
        ArrayList<Float> a = new ArrayList<Float>();
        
         String s = is.readLine();
         s.trim();
         String b[] = s.split(" ");
         System.out.println(b.length);
        
         for (int i=0; i<b.length; i++) {
         System.out.println(b[i]); 
         }
         //System.out.println(s);
          */
        
        
        /*
         * 下面這段代碼,處理大文件的讀寫。
         */
        BufferedReader is = new BufferedReader(new FileReader("srtm1-2"));
        ArrayList<Float> a = new ArrayList<Float>();
        int line = 2;
        for (int i=0; i<line; i++) {
         String s = is.readLine();
          s.trim();
          String b[] = s.split(" ");
          System.out.println(b.length);
         
          for (int j=0; j<b.length; j++) {
          System.out.println(b[j]); 
          }
          System.out.println("********************************");
        }
       }

      }

      国产一级婬片AAA毛,无码中文精品视视在线观看,欧美日韩a人成v在线动漫,五月丁香青草久久
      <code id="qf3hh"></code>
    • <menuitem id="qf3hh"></menuitem>
    • <strike id="qf3hh"><label id="qf3hh"></label></strike>