Java read Txt,java read large file

Source: Internet
Author: User

Java read Txt,java read large file

Package com.bbcmart.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 = 0x1200000;//buffer size is 12M

System.out.println (buffer_size);

File F = new file (config.tempdatafile);

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 12M 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");
}
}

Java read Txt,java read large 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.