简体   繁体   English

附加数据中的java.io.StreamCorruptedException

[英]java.io.StreamCorruptedException in appending data

I have written this java code for appending data in ObjectOutputStream, but this code is throwing (java.io.StreamCorruptedException:). 我已经编写了用于在ObjectOutputStream中追加数据的Java代码,但是该代码正在抛出(java.io.StreamCorruptedException :)。 Please help if this code can not work properly then please give an alternative for appending data in ObjectOutputStream. 如果此代码无法正常工作,请提供帮助,然后提供在ObjectOutputStream中追加数据的替代方法。

import java.awt.Toolkit;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Vector;
import javax.swing.JOptionPane;


class Data implements Serializable {

    private static final long serialVersionUID = 1L;
    private String time;
    private String note;

    public Data(String time, String note) {
        this.time=time;
        this.note=note;
    }

    public String getTime() {
        return time;
    }

    public String getNote() {
        return note;
    }
}


public class S extends ObjectOutputStream {

    String t, n;

    public S(FileOutputStream w, String time, String note) throws Exception {
        super(w);
        t=time;
        n=note;
        writeStreamHeader();
    }

    protected void writeStreamHeader() throws IOException {
        writeObject(new Data(t,n));
        reset();
     }


    public static void rd() {

        Vector v = new Vector();
        Data d;

        try
        {
            ObjectInputStream r = new ObjectInputStream(new FileInputStream("file.cer"));

            for(int i=1; i<=100; i++) {
                try { v.add(r.readObject()); }
                catch(EOFException exp){
                    r.close();
                    break;
                }
            }
            for(int i=0; i<v.size(); i++) {
                d = (Data)v.elementAt(i);
                System.out.println(d.getNote()+" "+d.getTime());
            }
        }

        catch(Exception exp) {
            Toolkit.getDefaultToolkit().beep();
            JOptionPane.showMessageDialog(null, String.format("ERROR = %s\nCLASS = S", exp.getClass()));
            System.out.println(exp.getClass());
            System.exit(0);
        }
    }

    public static void main(String arg[]) throws Exception {
        FileOutputStream w  = new FileOutputStream("file.cer",true);
        new S(w,"99:59:59:99","Maxima"); 
        new S(w,"00:00:00:00","Minima"); 
        rd();
    }
}

if this code can not work properly 如果此代码无法正常工作

You are correct. 你是对的。 It can not work properly. 它不能正常工作。

then please give an alternative for appending data in ObjectOutputStream. 然后请提供在ObjectOutputStream中附加数据的替代方法。

writeStreamHeader() must do nothing if the file is being appended to, but it must call super.writeStreamHeader() if the file is newly created. 如果要追加文件,则writeStreamHeader()不能执行任何操作 ,但是如果是新创建的文件,则必须调用super.writeStreamHeader() It certainly should not call writeObject() at any time. 当然,任何时候都不应调用writeObject()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM