java序列化讀取與文字檔案讀取資料效率對比

2021-04-19 10:54:22 字數 859 閱讀 6564

分別寫入資料到序列化檔案和文字檔案中:

public

static

void main(string args) 

fw.close();

fileoutputstream filestream = new fileoutputstream("e://s.obj"); 

objectoutputstream out = new objectoutputstream(filestream); 

out.writeobject(al);

out.close(); 

} catch (exception e)    

} 然後寫程式讀取序列化檔案和文字檔案,並將資料賦值到arraylist中。

序列化讀取:

public

static

void main(string args)  catch (exception e)  

}public

static

void main(string args) 

br.close();

fw.close();

system.out.println(system.currenttimemillis()-t);

} catch (exception e) 

} 結論:

在行數i=1000時,序列化讀取平均32,文字檔案讀取平均16。

在行數i=10000時,序列化讀取平均46,文字檔案讀取平均46。

在行數i=100000時,序列化讀取平均96,文字檔案讀取平均316。

故在大資料量讀取時,使用序列化方式訪問資料,效率較高;而在小資料量(小於10000行)時,使用文字檔案訪問資料效率較高。

Java讀取超大文字檔案

近日由於相關業務,需要讀取乙個將近1g的檔案,然後將符合條件的資料insert進資料庫。而執行完程式之後,最後寫入資料庫的資料是將近100w條,在linux系統下執行時間將近3個小時,操作量還是上了乙個相當的規模。由於之前沒有進行過超大檔案的讀寫,一開始以為需要使用分布式系統等複雜的操作才能進行。後...

讀取文字檔案

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...