java讀取大文件代碼,備忘。
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) + "毫秒"); } }