Java讀取超大文字檔案

2021-08-25 16:25:21 字數 703 閱讀 3521

近日由於相關業務,需要讀取乙個將近1g的檔案,然後將符合條件的資料insert進資料庫。而執行完程式之後,最後寫入資料庫的資料是將近100w條,在linux系統下執行時間將近3個小時,操作量還是上了乙個相當的規模。

由於之前沒有進行過超大檔案的讀寫,一開始以為需要使用分布式系統等複雜的操作才能進行。後來google了一下,發現jdk本身就支援超大檔案的讀寫,真是虛驚一場。

網上的文章基本分為兩大類,一類是使用bufferedreader類讀寫超大檔案;另一類是使用randomaccessfile類讀取,經過比較,最後使用了前一種方式進行超大檔案的讀取,下面是相關**,其實很簡單

[code]

file file = new file(filepath);

bufferedinputstream fis = new bufferedinputstream(new fileinputstream(file));

bufferedreader reader = new bufferedreader(new inputstreamreader(fis,"utf-8"),5*1024*1024);// 用5m的緩衝讀取文字檔案

string line = "";

while((line = reader.readline()) != null)

[/code]

注意**,在例項化bufferedreader時,增加乙個分配快取的引數即可

C 讀取超大文字檔案

現有乙個超大txt檔案,內部以 t分格,每行244個值,一共250萬行,大小1.9g,第一行是列名,現在要將每一行的資料讀出來進行處理,如果一次性讀進記憶體肯定是不行的。目錄 讀第一行 讀後續行 遍歷方法 使用感受 參考 string headerline file.readlines path f...

讀取文字檔案

void ctestdlg onreadinfo cfile filewrite1 testwrite1.txt cfile modecreate cfile modewrite cfile filewrite2 testwrite2.txt cfile modecreate cfile modew...

讀取文字檔案內容

讀取文字檔案內容 param filepathandname 帶有完整絕對路徑的檔名 param encoding 文字檔案開啟的編碼方式 return 返回文字檔案的內容 public string readtxt string filepathandname,string encoding th...