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