Java reads more than 500M files, Java reads large files

Source: Internet
Author: User

Set the cache size Buffer_size, Config.tempdatafile is the file address

SOURCE Blog http://yijianfengvip.blog.163.com/blog/static/175273432201191354043148/

Package com.yjf.util;
Import Java.io.File;
Import Java.io.RandomAccessFile;
Import Java.nio.MappedByteBuffer;
Import Java.nio.channels.FileChannel;

public class Test {
public static void Main (string[] args) throws Exception {
Final int buffer_size = 0x300000; Buffer is 3M
File F = new file (config.tempdatafile);
SOURCE Blog http://yijianfengvip.blog.163.com/blog/static/175273432201191354043148/
int len = 0;
Long start = System.currenttimemillis ();
for (int z = 8; z >0; z--) {
Mappedbytebuffer InputBuffer = new Randomaccessfile (f, "R")
. Getchannel (). Map (FileChannel.MapMode.READ_ONLY,
F.length () * (z-1)/8, F.length () * 1/8);
byte[] DST = new byte[buffer_size];//reads 3M of content each time
for (int offset = 0; offset < inputbuffer.capacity (); offset + = buffer_size) {
if (inputbuffer.capacity ()-offset >= buffer_size) {
for (int i = 0; i < buffer_size; i++)
Dst[i] = inputbuffer.get (offset + i);
} else {
for (int i = 0; i < inputbuffer.capacity ()-offset; i++)
Dst[i] = inputbuffer.get (offset + i);
}
int length = (inputbuffer.capacity ()% Buffer_size = = 0)? Buffer_size
: inputbuffer.capacity ()% Buffer_size;
Len + = new String (DST, 0, length). Length ();
System.out.println (New String (DST, 0, length). Length () + "-" + (Z-1) + "-" + (8-z+1));
}
}
System.out.println (len);
Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Read File file Cost:" + (End-start) + "MS");
}

}

SOURCE Blog http://yijianfengvip.blog.163.com/blog/static/175273432201191354043148/

Java reads more than 500M files, Java reads large files

Java reads more than 500M files, Java reads large files

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.