java—将hashmap附加到objectoutputstream

flmtquvp  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(261)

我试图附加到对象的hashmap,但read方法的输出即使在附加之后也保持不变(第一次和第二次称为read方法)。我在网上搜索了关于追加objectoutputstream的内容,根据答案我覆盖了 writeStreamHeader 子类中的方法 AppendableObjectOutputStream ,但对我不起作用。

public class MapObject {

public static void main(String args[]) {
    new MapObject();
}

public MapObject() {
    output1();       //createing objectOutputStream
    read();          //first time called read method    //output 15  20  14  12
    output2();       //appending to objectOutputStream
    read();          //second time called read method    //output 15  20  14  12
}

public void output1() {
    Map<Integer, List<Integer>> listMap = new HashMap<>();
    listMap.put(1, new ArrayList<>());
    listMap.get(1).add(15);
    listMap.get(1).add(20);

    listMap.put(2, new ArrayList<>());
    listMap.get(2).add(14);
    listMap.get(2).add(12);

    try {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("map"))));
        objectOutputStream.writeObject(listMap);
        objectOutputStream.flush();
        objectOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void output2() {
    Map<Integer, List<Integer>> listMap = new HashMap<>();
    listMap.put(1, new ArrayList<>());
    listMap.get(1).add(11);
    listMap.get(1).add(12);

    listMap.put(2, new ArrayList<>());
    listMap.get(2).add(21);
    listMap.get(2).add(22);

    try {
        AppendableObjectOutputStream appendableObjectOutputStream = new AppendableObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("map"), true)));
        appendableObjectOutputStream.writeObject(listMap);
        appendableObjectOutputStream.flush();
        appendableObjectOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public void read() {
    ObjectInputStream objectInputStream = null;
    try {

        objectInputStream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("map"))));
        Map<Integer, List<Integer>> listMap = (Map<Integer, List<Integer>>) objectInputStream.readObject();
        listMap.values().forEach(v1 -> {
            v1.forEach(System.out::println);
        });

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }}

}

我发现objectoutputstream类的子类如下所示。

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

@Override
protected void writeStreamHeader() throws IOException {
}}

对不起,英语不好。

slhcrj9b

slhcrj9b1#

问题在于 read() 方法。您正在将多个对象写入objectoutputstream,因此,必须从相应的objectoutputstream中读取多个对象:

public void read() {
    ObjectInputStream objectInputStream = null;
    try {
        objectInputStream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("map"))));

        while(true) {
            Map<Integer, List<Integer>> listMap;
            try {
                listMap = (Map<Integer, List<Integer>>) objectInputStream.readObject();
            } catch(EOFException e) {
                break;
            }
            listMap.values().forEach(v1 -> {
                v1.forEach(System.out::println);
            });
        }
    } catch(IOException e) {
        e.printStackTrace();
    } catch(ClassNotFoundException e) {
        e.printStackTrace();
    }
}

相关问题