java属性hashmap读取
回答 我刚刚找到了我正在寻找的东西:
Properties properties = new Properties();
FileInputStream in = new FileInputStream("/somePath/file.map");
properties.load(in);
in.close();
HashMap<String, String> propMap = new HashMap<String, String>((Map) properties);
这使我能够将打开的属性数据返回到哈希图中,而无需知道属性名称。
原始问题。
我有以下代码可以写出 HashMap 的结果。我想知道备份打开此属性并将数据拉回 HashMap 的最简单方法,putAll 是将数据获取到属性并存储数据的好方法。我没有看到 getAll 来检索它,并且在创建哈希映射之前不知道 HashMap 键/值数据,因此不能仅通过属性名称进行检索。创建后的数据将是静态的,因此我可以物理打开基于哈希图写入的文件以获取属性名称,但宁愿不必这样做。感谢您的任何帮助。
Properties properties = new Properties();
properties.putAll(mapTabSets);
properties.store(new FileOutputStream("/somePath/file.map"),"Java properties);
Answer
I just found what I was looking for:
Properties properties = new Properties();
FileInputStream in = new FileInputStream("/somePath/file.map");
properties.load(in);
in.close();
HashMap<String, String> propMap = new HashMap<String, String>((Map) properties);
This allowed me to get the opened property data back into the hashmap with out needing to know the property names.
original question.
I have the following code that writes out the results of a HashMap. I'm wondering the easiest way to open this property back up and pull the data back into a HashMap, the putAll was a nice way to get the data into the property and store the data. I don't see a getAll to retrieve it, and the HashMap Key/Value data was not known before the hashmap was created so can't just retrieve by property name. The data after it was created will be static, so I could physically open the file written based off the hashmap to get the property names, but would rather not have to do it that way. Thanks for any help.
Properties properties = new Properties();
properties.putAll(mapTabSets);
properties.store(new FileOutputStream("/somePath/file.map"),"Java properties);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管属性没有 getAll 函数,但它们确实具有
这两个函数,这两个函数将提供属性的所有键的集合。然后,您可以迭代集合并通过以下方式从属性中提取所有值
Although properties do not have a getAll function, they do have
both of which will deliver a collection of all keys of the property. You can then iterate over the collection and extract all values from the property via
尝试以下操作。
Try the following.