这个问题已经在这里有了答案: > StreamCorruptedException: invalid type code: AC 1个
我试图在每次单击按钮时将一个对象(OneChatMessage类的对象)添加到文件中.然后,我尝试从文件中读取所有对象,并将其加载到ArrayList中.但是我在反序列化函数的Object@R_301_5983@Stream.readobject()的第二次迭代中得到了StreamCorruptedException.我已经验证了第一次迭代可以正常工作.
我已经读过许多关于同一问题的其他文章:
>一个建议(Deserialize multiple Java Objects)是将整个数组列表而不是单个对象写入文件.但这似乎很浪费,因为每次单击按钮时我都需要写入文件,并且随着时间的推移将有数百个对象.
>另一个建议(StreamCorruptedException: invalid type code: AC)与ObjectOutputStream创建的不同标头有关.我不知道该用谁来解决这个问题.
关于如何解决这个问题的任何想法吗?
OneChatMessage.java:
@H_301_15@package com.slapp.chat;import java.io.Serializable;import java.util.Date;import com.slapp.localDB.Contact;//This class defines the class for a chat messagepublic class OneChatMessage implements Serializable{ private String text; private Date timeSent; private Boolean sentByMe; public OneChatMessage(String text, Date timeSent, Boolean sentByMe){ this.text = text; this.timeSent = timeSent; this.sentByMe = sentByMe; } public String getText(){ return this.text; } public Date getTimeSent(){ return this.timeSent; } @OverrIDe public String toString(){ return new StringBuffer(" Text : ") .append(this.text) .append(" TimeSent : ") .append(this.timeSent.toString()) // Need to use SimpleDatFormat to get the correct time zone and formatting. Leaving it for Now. .append(" SentByMe : ") .append(this.sentByMe.toString()).toString(); }}
序列化功能:
@H_301_15@public voID addMessage(OneChatMessage msg) { fileOutputStream fos; ObjectOutputStream oos; file file = new file(context.getfilesDir(), "abc"); if (!file.exists()) { Log.d("faizal", "The file does not exist : "); } else { Log.d("faizal", "The file already exists : "); } try { fos = context.openfileOutput("abc", Context.MODE_APPEND); oos = new ObjectOutputStream(fos); oos.writeObject(msg); oos.close(); fos.close(); } catch (fileNotFoundException e) { Log.d("faizal", "file not found : "); e.printstacktrace(); } catch (IOException e) { Log.d("faizal", "Error closing file : "); e.printstacktrace(); }}
反序列化功能:
@H_301_15@public ArrayList<OneChatMessage> getAllChatMessagesArrayList() { ArrayList<OneChatMessage> chatMessages = new ArrayList<OneChatMessage>(); OneChatMessage oneChatMsg; file@R_301_5983@Stream fis; Object@R_301_5983@Stream ois; try { fis = context.openfile@R_301_5983@("abc"); ois = new Object@R_301_5983@Stream(fis); //Setting an arbitrarily large number as the limit of the loop iterations //so that chat message are read from the file till EOF for (int i = 1; i < 999999999; i++) { try { oneChatMsg = (OneChatMessage) ois.readobject(); } catch (EOFException e) { //When EOFException occurs, quit the loop break; } try{ chatMessages.add(oneChatMsg); } catch(Exception e){ Log.d("faizal","Error adding message to Array List:" + oneChatMsg.getText()); e.printstacktrace(); } } ois.close(); fis.close(); return chatMessages; } catch (fileNotFoundException e) { Log.d("faizal", "The file not found : "); e.printstacktrace(); } catch (IOException e) { Log.d("faizal", "Error closing file : "); e.printstacktrace(); } catch (ClassNotFoundException e) { Log.d("faizal","Error reading object from file : "); e.printstacktrace(); } catch (UnsupportedOperationException e) { Log.d("faizal", "Error adding object to array List from file : "); e.printstacktrace(); } return null; // in case there was a caught exception }
Logcat:
@H_301_15@05-27 16:56:17.576: W/System.err(25028): java.io.StreamCorruptedException: Wrong format: ac05-27 16:56:17.576: W/System.err(25028): at java.io.Object@R_301_5983@Stream.corruptStream(Object@R_301_5983@Stream.java:701)05-27 16:56:17.576: W/System.err(25028): at java.io.Object@R_301_5983@Stream.readNonPrimitiveContent(Object@R_301_5983@Stream.java:814)05-27 16:56:17.576: W/System.err(25028): at java.io.Object@R_301_5983@Stream.readobject(Object@R_301_5983@Stream.java:2006)05-27 16:56:17.576: W/System.err(25028): at java.io.Object@R_301_5983@Stream.readobject(Object@R_301_5983@Stream.java:1963)05-27 16:56:17.576: W/System.err(25028): at com.slapp.chat.ChatPersistenceHandler.getAllChatMessagesArrayList(ChatPersistenceHandler.java:62)05-27 16:56:17.576: W/System.err(25028): at com.example.slapp.ChatSessionActivity$ChatArrayAdapter.<init>(ChatSessionActivity.java:178)05-27 16:56:17.576: W/System.err(25028): at com.example.slapp.ChatSessionActivity.onCreate(ChatSessionActivity.java:86)05-27 16:56:17.576: W/System.err(25028): at androID.app.Activity.performCreate(Activity.java:5372)05-27 16:56:17.576: W/System.err(25028): at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)05-27 16:56:17.576: W/System.err(25028): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2270)05-27 16:56:17.576: W/System.err(25028): at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)05-27 16:56:17.576: W/System.err(25028): at androID.app.ActivityThread.access0(ActivityThread.java:168)05-27 16:56:17.576: W/System.err(25028): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)05-27 16:56:17.576: W/System.err(25028): at androID.os.Handler.dispatchMessage(Handler.java:99)05-27 16:56:17.576: W/System.err(25028): at androID.os.Looper.loop(Looper.java:137)05-27 16:56:17.576: W/System.err(25028): at androID.app.ActivityThread.main(ActivityThread.java:5493)05-27 16:56:17.576: W/System.err(25028): at java.lang.reflect.Method.invokeNative(Native Method)05-27 16:56:17.576: W/System.err(25028): at java.lang.reflect.Method.invoke(Method.java:525)05-27 16:56:17.576: W/System.err(25028): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)05-27 16:56:17.576: W/System.err(25028): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:1025)05-27 16:56:17.576: W/System.err(25028): at dalvik.system.NativeStart.main(Native Method)
解决方法:
追加到ObjectOutputStream不能立即使用,因为它会创建多个标头,如StreamCorruptedException: invalid type code: AC中所述.
有关解决方案,请参见Appending to an ObjectOutputStream.
总结以上是内存溢出为你收集整理的java.io.StreamCorruptedException:读取多个对象时格式错误全部内容,希望文章能够帮你解决java.io.StreamCorruptedException:读取多个对象时格式错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)