java - 迭代追加到 ObjectOutputStream

标签 java objectinputstream objectoutputstream

我想向文件添加大量数据。我定义了 HYB 类,因为我的对象包含不同类型的数据(String 和 byte[])。我使用 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() 调用包装在一个读取/写入的循环中,直到没有其他内容可从流中读取。

关于java - 迭代追加到 ObjectOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24869417/

相关文章:

java - 在开始标签中指定plid与在Liferay中将其作为参数传递之间的区别

java - 如何在 Serialized 实现中序列化绝对最小量的数据?

java - 流 - ObjectOutputStream 给出 NULL

java - 为什么我的 ObjectInputStream 抛出 EOFException?

java - 在 Java 中使用 Externalizable 接口(interface)反序列化

java - 从宾果游戏方法中压缩代码

java - 如何获取另一个类中JRadioButton MenuItem的状态?

java - 防止Linux子进程在父进程终止时终止

java - 初始化 ObjectInputStream 会导致 IOException 类型的 EOFException

java - 服务器没有收到来自客户端的更多消息