我找到了从文件(磁盘上)读取 hashMap 的代码:
public HashMap<String, Integer> load(String path)
{
try
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
Object result = ois.readObject();
//you can feel free to cast result to HashMap<String, Integer> if you know that only a HashMap is stored in the file
return (HashMap<String, Integer>)result;
}
catch(Exception e)
{
e.printStackTrace();
}
}
但我没有找到任何示例这个文件是什么样子的。你能举个例子解释一下吗?
最佳答案
您需要使用 ObjectOutputStream
将其写出(请参阅文档 here)。
关于java - 如何从文件中读取hashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13029094/