我将超过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