gpt4 book ai didi

java - 如何将HashMap写入txt文件?

转载 作者:行者123 更新时间:2023-12-01 18:37:08 25 4
gpt4 key购买 nike

我有一个 HashMap,其中包含一些数据,我想将这些数据写入文件并重新加载文件内容以形成相同的 HashMap。

HashMap如下:

    HashMap<Long, List<DataPoint>> hashMap = new HashMap<Long, List<DataPoint>>();

DataPoint类如下:

public class DataPoint {

private int time;
private int songId;

public DataPoint(int songId, int time) {
this.songId = songId;
this.time = time;
}

public int getTime() {
return time;
}

public int getSongId() {
return songId;
}
}

非常感谢您的帮助。

谢谢

最佳答案

更改 DataPoint 以实现 java.io.Serializable 并使用 ObjectOutputStream.writeObject/ObjectInputStream.readObject。所有 Java SE 集合实现都是可序列化的

关于java - 如何将HashMap写入txt文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21508545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com