Java uses memory-mapped files to read files on rows

Source: Internet
Author: User

We know that memory-mapped file reads are the fastest in a variety of reading methods, but the memory-mapped file read API does not provide a read-by-line approach that needs to be implemented by itself. Here is how I use memory-mapped files to read files by line, if there are errors, please point out, or there is a better and faster way to implement the trouble is to provide code.

The code is as follows:

public class Testmemorymappedfile {public static void main (string[] agrs) throws Ioexception{randomaccessfile MemoryMappedFile = new Randomaccessfile ("D://test.txt", "R"); int size = (int) memorymappedfile.length (); Mappedbytebuffer out = Memorymappedfile.getchannel (). Map (filechannel.mapmode.read_only,0,size); Long start = System.currenttimemillis ();//To assign a value based on the average byte size of the file line final int extra = 200;int count = extra;byte[] buf = new Byte[count];int j=0; char ch = '% '; Boolean flag = False;while (out.remaining () >0) {byte by = Out.get (); ch = (char) by;switch (CH) {case ' \ n ': flag = True;break;case ' \ r ': flag = true;break;default:buf[j] = by;break;} j++;//read characters that exceed the size of the BUF array and require dynamic expansion if (flag ==false && j>=count) {count = count + Extra;buf = copyOf (Buf,count);} if (flag==true) {//Here the encoding depends on the actual encoding of the file string line = new String (buf, "utf-8"); System.out.println (line); flag = False;buf = Null;count = Extra;buf = new byte[count];j = 0;}} Processes the last read if (j>0) {string line = new String (buf, "utf-8"); System.out.println (line);} Long End = System.currenttimemillis ();    SYSTEM.OUT.PRINTLN ("Time Consuming:" + (End-start)); Memorymappedfile.close ();} Capacity of the expanded array public static byte[] CopyOf (byte[] original,int newlength) {byte[] copy = new Byte[newlength]; System.arraycopy (Original,0,copy,0,math.min (original.length,newlength)); return copy;}}

After testing, can reach 50m/s speed, still more than randomaccessfile by line read 100 times times faster than.

Note: byte[] buf the size of this byte array to dynamically expand, if fixed, the speed will be slower, especially if the setting is very large, it will be more slow.

Reference blog: https://www.ibm.com/developerworks/cn/java/l-javaio/index.html This blog is worth a look at the speed of each reading method to make a comparison, at the same time to achieve the optimization method

Java uses memory-mapped files to read files on rows

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.