问题
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