java.io.StreamCorruptedException: invalid stream header: 4D6F7374

廉价感情. 提交于 2019-12-13 09:20:43

问题


The Java code is

        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("src/Stringdata.txt"));    
        String s=(String)ois.readObject();
        System.out.println(s.toString());

        } catch (IOException e) {

            e.printStackTrace();
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        }

here am getting java.io.StreamCorruptedException: invalid stream header: 4D6F7374 this error please help me


回答1:


An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream. src/Stringdata.txt is probably not a file of serialized objects previously written using an ObjectOutputStream. You probably want to use InputStreamReader instead




回答2:


java.io.StreamCorruptedException: invalid stream header: 4D6F7374 

4D6F7374 is "Most".

This is not a file of serialized objects. It is a text file. Read it with BufferedReader.readLine().




回答3:


Your problem is: Server send/receive data using DataOutputStream/DataInputStream and you are trying to read it in the client side using ObjectOutputStream/ObjectInputStream

Just make sure Server/Client sockets are reading/writing using the same input/output stream types.

Khalil.



来源:https://stackoverflow.com/questions/16709017/java-io-streamcorruptedexception-invalid-stream-header-4d6f7374

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!