【发布时间】:2015-10-20 03:44:49
【问题描述】:
我将 40,000 多个对象存储到一个文本文件中。我的问题是从文本文件中读取所有对象太慢了。 1000 个对象的文本文件甚至需要 4349 毫秒。
这是从文本文件中读取对象。
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 吗?
-
尝试用缓冲的输入流包装它。 ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
标签: java file-handling