2

I have a very large HashMap of the format HashMap<String, List<String>>, and I want to serialize it using BufferedOutputStream because I think that it will be more efficient than with a regular OutputStream.

But how do I divide the HashMap in chunks of the size of the buffer? Should I just iterate through the HashMap?

4

1 回答 1

1

如果您打算写入本地文件,则需要链接FileOutputStream,BufferedOutputStreamObjectOutputStream. 通过以下设置BufferedOutputStream,应使用 8192 字节的默认缓冲区最小化对文件系统的直接写入。

Map<String, List<String>> data = new HashMap<>();
data.put("myKey", List.of("A", "B", "C"));

File outFile = new File("out.bin");
try (FileOutputStream fos = new FileOutputStream(outFile);
     BufferedOutputStream bos = new BufferedOutputStream(fos);
     ObjectOutputStream oos = new ObjectOutputStream(bos)) {
    oos.writeObject(data);
    oos.flush();
}

除非输出文件太大,否则不需要进一步分块。

于 2019-11-18T14:46:11.183 回答