我将超过40,000个对象存储在文本文件中。我的问题是从文本文件中读取所有对象太慢了。即使对于1,000个对象的文本文件,它也需要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");
有没有更快的方法来解决这个问题?