我將超過40,000個對象存儲到文本文件中。我的問題正在讀取文本文件中的所有對象太慢。它甚至需要4349毫秒的1,000個對象 - 文本文件。閱讀大對象文本文件的最快方法
這是從文本文件中讀取對象。
long startR = System.currentTimeMillis();
try{
ois = new ObjectInputStream(new FileInputStream(f));
code_from_file.clear();
Codes obj = new Codes();
while((obj = (Codes) ois.readObject()) != null){
if(obj instanceof Codes){
code_from_file.add(obj);
}
}
}catch (EOFException ex){
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally{
try {
if (ois != null){
ois.close();
}
} catch (IOException ex){
ex.printStackTrace();
}
}
long endR = System.currentTimeMillis();
System.out.println("Read code from file : " + (endR - startR) + "ms");
是否有解決這個問題的任何更快的方法?
你嘗試ehcache的的固定寬度的? –
嘗試用bufferedinputstream包裝它。 ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(f))); – Eddie