public static void main(String[] args)
{
ByteBuffer b=ByteBuffer.wrap("hao rena ".getBytes());
ByteArrayInputStream bao=new ByteArrayInputStream(b.array());
try {
ObjectInputStream oop=new ObjectInputStream(bao);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
直接就报错:java.io.StreamCorruptedException: invalid stream header: 68616F20 这个怎么改呢?

解决方案 »

  1.   

    ObjectInputStream是用来读取序列化后的对象的。但是你却给它输入一串自己写的字节。不符合序列化对象的数据结构。就报错了。
      

  2.   

    用ObjectInputStream读出对象不许是要用ObjectOutputStream写入的对象,否则会报错,用ObjectOutputStream写入会加入固定的头,用ObjectInputStream读出会验证固定的头。
    修改方式:
    public static void main(String[] args)
     {
     ByteArrayInputStream bao=new ByteArrayInputStream();
     ObjectOutputStream out=new ObjectOutputStream(bao);
     out.writeObject("hao rena ");
     try {
     ObjectInputStream oop=new ObjectInputStream(bao); } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     }
     }