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