如何写一个HashMap到一个txt文件? [英] How to write a HashMap to a txt file?

查看:550
本文介绍了如何写一个HashMap到一个txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a HashMap containing some data that I want to write to a file and reload the contents of the file to form the same HashMap.

HashMap中如下所示:

HashMap is as follows:

    HashMap<Long, List<DataPoint>> hashMap = new HashMap<Long, List<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;
}
 }

帮助是非常AP preciated。

Help is much appreciated.

感谢

推荐答案

更改数据点来实现java.io.Serializable和使用ObjectOutputStream.writeObject / ObjectInputStream.readObject。所有的Java SE集合实现序列化

Change DataPoint to implement java.io.Serializable and use ObjectOutputStream.writeObject / ObjectInputStream.readObject. All Java SE collection implementations are Serializable

这篇关于如何写一个HashMap到一个txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆