I/O Memory-mapped files

          Memory-mapped files allow you to create and modify files that are too big to bring into memory.
          With a memory-mapped file, you can pretend that the entire file is in memory and that you can access it by simply treating it as a very large array.
          /**
           * Creating a very large file using mapping
           * 
          @author WPeng
           *
           * 
          @since 2012-11-7
           
          */
          public class LargeMappedFiles {
              
          static int length = 0x8ffffff//128M
              public static void main(String[] args) throws FileNotFoundException, IOException {
                  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.println((
          char)out.get(i));
                  }
              }

          }

          To do both writing and reading, we start with a RandomAccessFile, get a channel for that file.
          And then call map() to produce a MppedByteBuffer, which is a particular kind of direct buffer.
          Note that you must specify the starting point and the length of  the region that you want to map in the file;
          this means that you have the option to map samller regions of a large file.

          The file createed with the preceding program is 128M long.
          which is probably larger than your OS will allow in memory at one time.
          the file appears to be accessible all at once because only portions of it are brought into memory.
          and other parts are swapped out.
          this way a very large file(up to 2 GB) can easily be modified.
          note that the file-mapping facilities of the underlying operating system are used to maximize performance.



          posted on 2012-11-07 14:59 鹽城小土包 閱讀(169) 評論(0)  編輯  收藏 所屬分類: J2EE

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案(14)

          文章分類(18)

          文章檔案(18)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 东乡族自治县| 乌兰县| 威远县| 灵丘县| 浮梁县| 万全县| 永安市| 云阳县| 龙口市| 天水市| 新蔡县| 霍城县| 三明市| 饶河县| 安丘市| 西安市| 读书| 大足县| 织金县| 大名县| 西城区| 乌拉特中旗| 南宁市| 盐边县| 资阳市| 章丘市| 广丰县| 鄂伦春自治旗| 乳源| 丹棱县| 临安市| 扎囊县| 太康县| 棋牌| 塔河县| 长沙县| 台中县| 多伦县| 松溪县| 美姑县| 会东县|