java—将hashmap附加到objectoutputstream

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

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

  1. public class MapObject {
  2. public static void main(String args[]) {
  3. new MapObject();
  4. }
  5. public MapObject() {
  6. output1(); //createing objectOutputStream
  7. read(); //first time called read method //output 15 20 14 12
  8. output2(); //appending to objectOutputStream
  9. read(); //second time called read method //output 15 20 14 12
  10. }
  11. public void output1() {
  12. Map<Integer, List<Integer>> listMap = new HashMap<>();
  13. listMap.put(1, new ArrayList<>());
  14. listMap.get(1).add(15);
  15. listMap.get(1).add(20);
  16. listMap.put(2, new ArrayList<>());
  17. listMap.get(2).add(14);
  18. listMap.get(2).add(12);
  19. try {
  20. ObjectOutputStream objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("map"))));
  21. objectOutputStream.writeObject(listMap);
  22. objectOutputStream.flush();
  23. objectOutputStream.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. public void output2() {
  29. Map<Integer, List<Integer>> listMap = new HashMap<>();
  30. listMap.put(1, new ArrayList<>());
  31. listMap.get(1).add(11);
  32. listMap.get(1).add(12);
  33. listMap.put(2, new ArrayList<>());
  34. listMap.get(2).add(21);
  35. listMap.get(2).add(22);
  36. try {
  37. AppendableObjectOutputStream appendableObjectOutputStream = new AppendableObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("map"), true)));
  38. appendableObjectOutputStream.writeObject(listMap);
  39. appendableObjectOutputStream.flush();
  40. appendableObjectOutputStream.close();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. public void read() {
  46. ObjectInputStream objectInputStream = null;
  47. try {
  48. objectInputStream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("map"))));
  49. Map<Integer, List<Integer>> listMap = (Map<Integer, List<Integer>>) objectInputStream.readObject();
  50. listMap.values().forEach(v1 -> {
  51. v1.forEach(System.out::println);
  52. });
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. } catch (ClassNotFoundException e) {
  56. e.printStackTrace();
  57. }}
  58. }

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

  1. public class AppendableObjectOutputStream extends ObjectOutputStream {
  2. public AppendableObjectOutputStream(OutputStream out) throws IOException {
  3. super(out);
  4. }
  5. @Override
  6. protected void writeStreamHeader() throws IOException {
  7. }}

对不起,英语不好。

slhcrj9b

slhcrj9b1#

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

  1. public void read() {
  2. ObjectInputStream objectInputStream = null;
  3. try {
  4. objectInputStream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("map"))));
  5. while(true) {
  6. Map<Integer, List<Integer>> listMap;
  7. try {
  8. listMap = (Map<Integer, List<Integer>>) objectInputStream.readObject();
  9. } catch(EOFException e) {
  10. break;
  11. }
  12. listMap.values().forEach(v1 -> {
  13. v1.forEach(System.out::println);
  14. });
  15. }
  16. } catch(IOException e) {
  17. e.printStackTrace();
  18. } catch(ClassNotFoundException e) {
  19. e.printStackTrace();
  20. }
  21. }
展开查看全部

相关问题