【发布时间】:2012-10-08 17:44:25
【问题描述】:
我得到一个文件personHashMap.ser,里面有一个HashMap。这是我如何创建它的代码:
String file_path = ("//releasearea/ToolReleaseArea/user/personHashMap.ser");
public void createFile(Map<String, String> newContent) {
try{
File file = new File(file_path);
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(newContent);
oos.flush();
oos.close();
fos.close();
}catch (Exception e){
System.err.println("Error in FileWrite: " + e.getMessage());
}
}
现在我希望,当程序运行时,所有五分钟都更新文件personHashMap.ser 仅使用更改的内容。所以我调用的方法:
public void updateFile(Map<String, String> newContent) {
Map<String, String> oldLdapContent = readFile();
if(!oldLdapContent.equals(ldapContent)){ // they arent the same,
// so i must update the file
}
}
但现在我不知道如何实现这一点。
只更新新内容对性能更好还是应该清理整个文件并再次插入新列表?
希望你能帮助我..
编辑:
HashMap 包括 street=Example Street。
但现在,这条新街叫New Example Street。现在我必须更新文件中的 HashMap。所以我不能只是追加新内容...
【问题讨论】:
-
System.err.println("Error in FileWrite: " + e.getMessage());- 这将丢失堆栈跟踪,并且您的程序不应该真正进行。最好将其重新抛出为RuntimeException。 -
@artbristol:谢谢,但首先我想运行代码,然后我会发现错误......
-
@dTDesing 是的,但是您的代码会使您在调试时更难捕获错误。
-
@dTDesign:文件有多大?是 MB 还是几 KB?也只在 Bigining/End 或任何地方进行更改?文件格式是什么?
-
@artbristol:嗯。你是对的:) 我会更新
标签: java file filereader