import java.io.*;public class TestObjectIO {
public static void main(String args[]) throws Exception {
/*T t = new T("kkkkkkk");
t.k = 8;
FileOutputStream fos = new FileOutputStream("f:/testobjectio.txt", true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(t);
oos.flush();
oos.close();*/注释

FileInputStream fis = new FileInputStream("f:/testobjectio.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
T tReaded = (T)ois.readObject();
T tReaded1 = (T)ois.readObject();
T tReaded2 = (T)ois.readObject();
System.out.println(tReaded.s + " " + tReaded.s + " " + tReaded.s + " "  + tReaded.d);

}
}class T 
implements Serializable
{
T(String s)
{
this.s = s;
}
String s = null;
int i = 10;
int j = 9;
double d = 2.3;
transient int k = 15;
}
我用注释里的代码写了一个保存类的文件 里面保存了4个T类 将其注释掉后在下面想读出 可出现了如下错误
Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at TestObjectIO.main(TestObjectIO.java:16)
请高手指导下~另对于ObjectInputStream封装的文件里的指针不太清楚 麻烦帮解释下 如何控制读取文件中类的顺序

解决方案 »

  1.   

    恩 是在文件中事先存了3个对象 存了3次 想一次读出 找到问题了 是writeStreamHeader()的问题 可到了重写header的时候又出现问题了,下面是改进的
    import java.io.*;public class TestObjectIO {
    public static void main(String args[]) throws Exception {
    File f = new File("f:/dance.txt");
    int flag = 0;
    T t = new T();
    t.k = 8;
    FileOutputStream fos = new FileOutputStream(f, true);
    if (f.length() == 0)
    {
    flag = 1;
    }
    FileObjectOutputStream oos = new FileObjectOutputStream(fos,flag);
    oos.writeObject(t);
    oos.flush();
    oos.close();

    FileInputStream fis = new FileInputStream(f);
    ObjectInputStream ois = new ObjectInputStream(fis);
    T tReaded = (T)ois.readObject();
    System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);

    }
    }class T 
    implements Serializable
    {
    int i = 10;
    int j = 9;
    double d = 2.3;
    transient int k = 15;
    }class FileObjectOutputStream extends ObjectOutputStream
    {
    private int flag = 0;
    FileObjectOutputStream(OutputStream os, int i) throws Exception
    {
    super(os);
    flag = i;
    }
    protected void writeStreamHeader()
        throws IOException{
    if (flag == 0)
    {
    super.reset();
    }
    else
    {
    super.writeStreamHeader();
    }
    }
    }
    可好像还不对~~~ 对reset()的使用不太了解 能讲下它的使用和作用吗???
      

  2.   

    T t=new T("kkkkkkk"); 
    t.k=8; 
    FileOutputStream fos=new FileOutputStream("f:/testobjectio.txt",true); 
    ObjectOutputStream oos=new ObjectOutputStream(fos); 
    oos.writeObject(t); 
    oos.flush(); 
    oos.close();*/
    上面代码每次写入对象的时候把以前写的覆盖了,所以你保存的文件里面是没有四个T对象的。
    这样你再次读入的时候,后面两个readObject()读不到的,里面文件指针已经读入为null!
      

  3.   

    次数问题.import java.io.*;public class TestObjectIO {
    public static void main(String args[]) throws Exception {
    T t = new T("kkkkkkk");
    t.k = 8;
    FileOutputStream fos = new FileOutputStream("d:/testobjectio.txt");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(t);
    oos.flush();
    oos.writeObject(t);
    oos.flush();
    oos.writeObject(t);
    oos.flush();
    oos.close(); FileInputStream fis = new FileInputStream("d:/testobjectio.txt");
    ObjectInputStream ois = new ObjectInputStream(fis);
    T tReaded = (T) ois.readObject();
    T tReaded1 = (T) ois.readObject();
    T tReaded2 = (T) ois.readObject();
    System.out.println(tReaded.s + "   " + tReaded.s + "   " + tReaded.s
    + "   " + tReaded.d);
    System.out.println(tReaded1.s + "   " + tReaded1.s + "   " + tReaded1.s
    + "   " + tReaded1.d);
    System.out.println(tReaded2.s + "   " + tReaded2.s + "   " + tReaded2.s
    + "   " + tReaded2.d); }
    }class T implements Serializable {
    T(String s) {
    this.s = s;
    } String s = null;
    int i = 10;
    int j = 9;
    double d = 2.3;
    transient int k = 15;
    }
      

  4.   

    在T类中重写String toString方法,解决问题
    public String toString(){
    return this.s+this.d+this.i+this.j+this.k;
    }