Memory-mapped file mode (Mappedbytebuffer) handles an instance of a file

Source: Internet
Author: User

Package org.phoenix.cases.kafka;import java.io.file;import java.io.filenotfoundexception;import  java.io.FileReader;import java.io.IOException;import java.io.RandomAccessFile;import  java.nio.mappedbytebuffer;import java.nio.channels.filechannel;import org.apache.commons.io.ioutils; import org.apache.commons.io.lineiterator;import org.junit.test;/** *  a memory-mapped file to handle an instance of a file  *  @author  mengfeiyang * */public class mappedbytes { string  filePath =  "e:\\ working directory \ \ New project \ \ Feng Dance phase \\backup\\new_show_style_json.txt";   /**   *  instance of the memory-mapped file processing file   *  @throws  FileNotFoundException  *  @throws  IOException  */  @SuppressWarnings ("resource")   @Test  public void  Testmappedbyte ()  throws filenotfoundexception, ioexception {  long start  = system.currenttimemilliS ();   file file = new file (FilePath);   long filelength =  file.length ();   final int buffer_size=0x500000;//5m  mappedbytebuffer  inputbuffer = new randomaccessfile (file, "RW"). Getchannel (). Map (FileChannel.MapMode.READ_ Write, 0, filelength);  byte[] dst = new byte[buffer_size];   int count=0;  for (Int offset=0;offset<filelength;offset += buffer_size) {            if  (fileLength -  Offset >= buffer_size)  {                     for  (int i = 0; i <  buffer_size; i++)                     &nbsP;    dst[i] = inputbuffer.get (offset + i);                 } else {                     for   (int i = 0; i < filelength - offset; i++)                           dst[i] = inputbuffer.get (offset + i);                 }                 string bs = new string (DST, " UTF-8 ");//Convert bytes in buffer to string             string[]  ns = bs.split ("\ n");Nbsp;           for (String s : ns) {              if (S.contains (")") {               count++;               system.out.println (S.split ("a") [0]);              }             }                        system.out.println ();             //string s = ioutils.tostring (new  Bytearrayinputstream (DST));             // System.out.println (s);   } &nBsp SYSTEM.OUT.PRINTLN ("Total number of processing strips:" +count);   long end = system.currenttimemillis ();    system.out.println ((End - start)/1000);//processing 809M files, 90,000 data, only 6 seconds  }  }}


Memory-mapped file mode (Mappedbytebuffer) handles an instance of a file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.