Java 带有objectstream和hashmap的eofExeption

Java 带有objectstream和hashmap的eofExeption,java,hashmap,objectinputstream,objectoutputstream,eofexception,Java,Hashmap,Objectinputstream,Objectoutputstream,Eofexception,因此,我在从objectstream存储或加载hashmap时遇到问题。该文件已保存,我可以看到它存储了某种信息。问题是,当我尝试读取hashmap时,它会给我一个EOF错误。提前感谢你的帮助 public synchronized void store(StoredObject storedObject) { try { String objectName = storedObject.getObject().getClass().getName();

因此,我在从objectstream存储或加载hashmap时遇到问题。该文件已保存,我可以看到它存储了某种信息。问题是,当我尝试读取hashmap时,它会给我一个EOF错误。提前感谢你的帮助

public synchronized void store(StoredObject storedObject) {

    try {
        String objectName = storedObject.getObject().getClass().getName();

        File file = new File(LOCAL_STORAGE_PATH + objectName);
        boolean isNewFile = !file.exists();


        output = new ObjectOutputStream(new FileOutputStream(file));
        input = new ObjectInputStream(new FileInputStream(file));
        if (isNewFile) {
            localStorage = new HashMap<String, StoredObject>();
        } else {
            /////////////////////////
            ////does not work ///////
            /////////////////////////
            localStorage = (HashMap) input.readObject();
        }


        localStorage.put(storedObject.getKey(), storedObject);

        output.writeObject(localStorage);;
        output.flush();

    } catch (IOException ex) {
        //Logger.getLogger(LocalServer.class.getName()).log(Level.SEVERE, "ioexception", ex);
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(LocalServer.class.getName()).log(Level.SEVERE, "class missmatch", ex);
    }
    finally{
         try {
            output.close();
            input.close();
        } catch (IOException ex1) {
            Logger.getLogger(LocalServer.class.getName()).log(Level.SEVERE, "could not close connection", ex1);
        }
    }

}
公共同步作废存储(StoredObject StoredObject){
试一试{
字符串objectName=storedObject.getObject().getClass().getName();
文件文件=新文件(本地存储路径+对象名);
布尔值isNewFile=!file.exists();
输出=新对象输出流(新文件输出流(文件));
输入=新对象输入流(新文件输入流(文件));
if(isNewFile){
localStorage=newhashmap();
}否则{
/////////////////////////
////不起作用///////
/////////////////////////
localStorage=(HashMap)input.readObject();
}
localStorage.put(storedObject.getKey(),storedObject);
output.writeObject(localStorage);;
output.flush();
}捕获(IOEX异常){
//Logger.getLogger(LocalServer.class.getName()).log(Level.severy,“ioexception”,ex);
例如printStackTrace();
}捕获(ClassNotFoundException ex){
Logger.getLogger(LocalServer.class.getName()).log(Level.severy,“class missmatch”,ex);
}
最后{
试一试{
output.close();
input.close();
}捕获(IOException ex1){
Logger.getLogger(LocalServer.class.getName()).log(Level.SEVERE,“无法关闭连接”,ex1);
}
}
}
错误消息:

java.io.EOFException 位于java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2601) 位于java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1319) 位于java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) 位于ServiceLayer.LocalServer.LocalServer.store(LocalServer.java:66) 位于ServiceLayer.LocalServer.LocalServerTEST.main(LocalServerTEST.java:17)

我试过的东西:
让它同步。对象(StoredObject)和子对象实现可序列化。

您的输出和输入代码混合在一起。您同时创建输出流和输入流。当您调用
新文件输出流(file)
时,它会替换现有文件,因此当您尝试读取它时,它是空的

将输出和输入代码移动到两种不同的方法中