import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class ReadBig {
public static String fff = "D:\\report\\inbound\\Citi_OrderLines.csv";
public static void main1(String[] args) throws Exception {
final int BUFFER_SIZE = 0x300000;// 緩衝區大小爲3M
File f = new File(fff);
/**
* map(FileChannel.MapMode mode,long position, long size) mode -
* 根據是按只讀、讀取/寫入或專用(寫入時拷貝)來映射文件,分別爲 FileChannel.MapMode 類中所定義的
* READ_ONLY、READ_WRITE 或 PRIVATE 之一 position - 文件中的位置,映射區域今後位置開始;必須爲非負數
* size - 要映射的區域大小;必須爲非負數且不大於 Integer.MAX_VALUE
* 因此若想讀取文件後半部份內容,如例子所寫;若想讀取文本後1
* /8內容,須要這樣寫map(FileChannel.MapMode.READ_ONLY,
* f.length()*7/8,f.length()/8)
* 想讀取文件全部內容,須要這樣寫map(FileChannel.MapMode.READ_ONLY, 0,f.length())
*/
MappedByteBuffer inputBuffer = new RandomAccessFile(f, "r").getChannel().map(FileChannel.MapMode.READ_ONLY,
f.length() / 2, f.length() / 2);
byte[] dst = new byte[BUFFER_SIZE];// 每次讀出3M的內容
long start = System.currentTimeMillis();
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;
System.out.println(new String(dst, 0, length));// new
// String(dst,0,length)這樣能夠取出緩存保存的字符串,能夠對其進行操做
}
long end = System.currentTimeMillis();
System.out.println("讀取文件文件一半內容花費:" + (end - start) + "毫秒");
}
}java