我想向文件添加大量数据。我定义了HYB类,因为我的对象包含不同类型的数据(字符串和字节[])。我使用ObjectOutputStream和ObjectInputStream来读写文件。但是我的代码无法打印出预期的结果。为了编写代码,我在以下页面中使用了代码:
How can I append to an existing java.io.ObjectStream?

ClassCastException when Appending Object OutputStream

我尝试调试我的代码,但发现了问题,但没有。这是我的代码:

import java.io.*;
import java.io.BufferedOutputStream;
import java.util.*;

public class HYB implements Serializable
 {
private static final long serialVersionUID = 1L;
private List<byte[]> data = new ArrayList<>();

public void addRow(String s,byte[] a)
{
    data.add(s.getBytes()); // add encoding if necessary
    data.add(a);
}

@Override public String toString()
{
    StringBuilder sb = new StringBuilder();
    synchronized (data)
    {
        for(int i=0;i<data.size();i+=2)
        {
            sb.append(new String(data.get(i)));
            sb.append(Arrays.toString(data.get(i+1))+"\n");
        }
    }
    return sb.toString();
}

private static void write(File storageFile, HYB hf)
        throws IOException {
               ObjectOutputStream oos = getOOS(storageFile);
               oos.writeObject(hf);
               oos.flush();
               oos.close();
}

public static ObjectOutputStream getOOS(File file) throws IOException
{

    if (file.exists()) {
        return new AppendableObjectOutputStream(new FileOutputStream(file, true));
    } else {
        return new ObjectOutputStream(new FileOutputStream(file));
    }
}



 private static ObjectInputStream getOIS(FileInputStream fis)
        throws IOException {
               long pos = fis.getChannel().position();
               return pos == 0 ? new ObjectInputStream(fis) :
                         new AppendableObjectInputStream(fis);
}

private static class AppendableObjectOutputStream extends
ObjectOutputStream {

  public AppendableObjectOutputStream(OutputStream out)
    throws IOException {
        super(out);
                        }

  @Override
  protected void writeStreamHeader() throws IOException {

  }
 }

private static class AppendableObjectInputStream extends ObjectInputStream {

    public AppendableObjectInputStream(InputStream in) throws IOException {
        super(in);
    }

    @Override
    protected void readStreamHeader() throws IOException {
        // do not read a header
    }
}



public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException
{

    File x=new File ("test");


    HYB hf1 = new HYB();
    hf1.addRow("fatemeh",new byte[] {11,12,13});
    hf1.addRow("andisheh",new byte[] {14,15,16});

    write(x,hf1);


    HYB hf = new HYB();
    hf.addRow("peter",new byte[] {1,2,3});
    hf.addRow("jaqueline",new byte[] {4,5,6});
    write(x,hf);


    FileInputStream fis = new FileInputStream(x);
    HYB hf2 = (HYB) getOIS(fis).readObject();
    System.out.println(hf2);
  }
}


预期成绩:

fatemeh[11, 12, 13]
andisheh[14, 15, 16]
peter[1, 2, 3]
jaqueline[4, 5, 6]


实际结果:

fatemeh[11, 12, 13]
andisheh[14, 15, 16]

最佳答案

将两个HYB对象写入ObjectOutputStream不会将它们合并为一个HYB对象; ObjectOutputStream仍然包含两个HYB对象,您的代码读取其中一个。如果第二次调用readObject(),则第二个将被检索并可以打印到屏幕上。因此,您可以将readObject()println()调用包装在一个可读写的循环中,直到从流中读取其他内容为止。

10-06 03:25