gpt4 book ai didi

java - 如何从文件中读取hashMap

转载 作者:行者123 更新时间:2023-11-29 05:53:33 25 4
gpt4 key购买 nike

我找到了从文件(磁盘上)读取 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/

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