1
回答
关于利用ReadObject反序列化时报EOF错误的问题
滴滴云服务器,限时包月0.9元,为开发者而生>>>   

现在程序需要实现的目标是,使用WriteFile来将userSession这个对象写入到文件当中,然后再使用ReadFile来从文件中读出userSession这个对象。(PS:Session对象已经序列化)


private HashMap<String, Session> userSession = new HashMap<String, Session>();
public void WriteFile() throws IOException{ 
    FileOutputStream outStream = new FileOutputStream(file);   
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream);    
    try {
        objectOutputStream.writeObject(userSession);
    } catch (IOException e) {
        e.printStackTrace();
    }   
    outStream.close();   
}


public boolean ReadFile() throws Exception { 
    if (!filecheck()) return false;
    FileInputStream freader;
    freader = new FileInputStream(file);   
    ObjectInputStream objectInputStream = new ObjectInputStream(freader);    
    userSession = (HashMap<String, Session>) objectInputStream.readObject(); 
    objectInputStream.close();
    return true;
}
程序运行状况是:之前一直运行良好,突然有一天,就出现了以下为报错信息。
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source)
at java.io.ObjectInputStream.readString(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.util.HashMap.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.nfjd.rmcp.model.UserSession.ReadFile(UserSession.java:113)
at com.nfjd.rmcp.model.UserSession.setUserSession(UserSession.java:44)
at com.jfinal.weixin.sdk.jfinal.MsgController.index(MsgController.java:108)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.jfinal.core.ActionInvocation.invoke(ActionInvocation.java:55)
at com.jfinal.weixin.sdk.jfinal.MsgInterceptor.intercept(MsgInterceptor.java:44)
at com.jfinal.core.ActionInvocation.invoke(ActionInvocation.java:51)
at com.jfinal.core.ActionHandler.handle(ActionHandler.java:73)
at com.jfinal.ext.handler.ContextPathHandler.handle(ContextPathHandler.java:47)
at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:365)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:642)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Unknown Source)

我把服务器上保存的那个文件拷贝了下来,在本地debug也是会报这个错。我把原来那个文件删除,让程序自动生成新的文件,然后进行读写就不会有问题。难道跟文件的大小有关系?





举报
33号小人物
发帖于2年前 1回/748阅
共有1个答案 最后回答: 2年前

      序列化与反序列化有一个类似于类文件的版本问题需要注意。原因是,例如你有一个类文叫 User,里面有一个字段叫 userName,此时你对 User 进行序列化与反序列化都是正常的,后来你为 User 添加了一个 age 属性,此时以前被序列化的数据再反序列化为 User 时,发现不对头了,因为新创建的 age 属性没东西。

    所以序列化功能在实现 Serializable 接口时,需要定义一个 private static final long serialVersionUID 这样的常量,就是用来标识类文件版本的,不指定版本时系统会根据默认算法为你指定一个,当反序列化时版本不同就会报异常,实属正常现象,将原来的数据清空,再重新写入即可。

--- 共有 1 条评论 ---
糊搞一直不知道这个自动生成的ID是干什么用的,这回知道了。。。 谢波总。。。 2年前 回复
顶部