InputStream is=new ByteArrayInputStream("asdfsad".getBytes());
ObjectInputStream ois = new ObjectInputStream(is);结果却报出这样异常
java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:737)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:253)
请问各位高手

解决方案 »

  1.   

    ObjectInputStream 参数是输入流,字节数组不能被识别为输入流。
    楼主可以试着建立对像类
    例如:
    class TempObj implements Serializable
    {
    private String str = "asdfsad";
    public String getStr(){return this.str;}
    public void setString(String str){this.str = str;}
    }
    然后在
    对网络或其它文件中获得TempObj对象来进行操作。
      

  2.   

    ByteArrayInputStream和ObjectInputStream 不匹配
      

  3.   

    InputStream is=new ByteArrayInputStream("asdfsad".getBytes());
    这句其实将字节数组封装为 流格式 的
    ObjectInputStream ois = new ObjectInputStream(is);
    参数是inputStream流
      

  4.   

    FileInputStream fis = new FileInputStream("d://abc.txt");
     ObjectInputStream ois = new ObjectInputStream(fis);
    也是报出同样的异常
      

  5.   

    ByteArrayInputStream 作为参数提供给 ObjectInputStream 是没有问题的,你的程序唯一不对的地方是不应该将 "asdfsad".getBytes() 做为输入参数提供给 ByteArrayInputStream ,ObjectInputStream 是要读取对象的,那么自然输入也应该是一个对象的字节数组,而很明显 String.getBytes 并不能返回作为字符串对象的字节数组,仅是该字符串对象内容的字节数组。    ByteArrayOutputStream bos = new ByteArrayOutputStream(100);
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject("asdfsad");
        
        byte [] bytes = bos.toByteArray();
        InputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bis);
        Object obj = ois.readObject();    System.out.println("obj : " + obj);