java 我们可以将哈希表写入文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2808277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:57:05  来源:igfitidea点击:

Can we write a Hashtable to a file?

javahashtable

提问by tiendv

I have a Hashtable<string,string>, in my program I want to record the values of the Hashtable to process later.

我有一个Hashtable<string,string>, 在我的程序中我想记录 Hashtable 的值以供以后处理。

My question is: can we write object Hastable to a file? If so, how can we later load that file?

我的问题是:我们可以将对象 Hastable 写入文件吗?如果是这样,我们以后如何加载该文件?

采纳答案by Bozho

Yes, using binary serialization (ObjectOutputStream):

是的,使用二进制序列化 ( ObjectOutputStream):

FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(yourHashTable);
oos.close();

Then you can read it using ObjectInputStream

然后你可以阅读它使用 ObjectInputStream

The objects that you put inside the Hashtable(or better - HashMap) have to implement Serializable

您放入Hashtable(或更好 - HashMap)的对象必须实现Serializable



If you want to store the Hashtablein a human-readable format, you can use java.beans.XMLEncoder:

如果要以Hashtable人类可读的格式存储 ,可以使用java.beans.XMLEncoder

FileOutputStream fos = new FileOutputStream("tmp.xml");
XMLEncoder e = new XMLEncoder(fos);
e.writeObject(yourHashTable);
e.close();

回答by aioobe

Don't know about your specific application, but you might want to have a look at the Properties class. (It extends hashmap.)

不知道您的特定应用程序,但您可能想查看Properties 类。(它扩展了哈希图。)

This class provides you with

本课程为您提供

void  load(InputStream inStream)
     Reads a property list (key and element pairs) from the input byte stream.
void  load(Reader reader)
     Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.
void  loadFromXML(InputStream in)
     Loads all of the properties represented by the XML document on the specified input stream into this properties table.
void  store(Writer writer, String comments)
      Writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) method.
void  storeToXML(OutputStream os, String comment)
      Emits an XML document representing all of the properties contained in this table.
void  load(InputStream inStream)
     Reads a property list (key and element pairs) from the input byte stream.
void  load(Reader reader)
     Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.
void  loadFromXML(InputStream in)
     Loads all of the properties represented by the XML document on the specified input stream into this properties table.
void  store(Writer writer, String comments)
      Writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) method.
void  storeToXML(OutputStream os, String comment)
      Emits an XML document representing all of the properties contained in this table.

The tutorialis quite educational also.

该教程也很有教育意义。

回答by wds

If you want to be able to easily edit the map once it's written out, you might want to take a look at jYaml. It allows you to easily write the map to a Yaml-formatted file, meaning it's easy to read and edit.

如果您希望在写出地图后能够轻松对其进行编辑,您可能需要查看jYaml。它允许您轻松地将地图写入 Yaml 格式的文件,这意味着它易于阅读和编辑。

回答by Andrejs

You could also use MapDBand it will save the HashMap for you after you do a putand a commit. That way if the program crashes the values will still be persisted.

您也可以使用MapDB,它会在您执行 aput和 a后为您保存 HashMap commit。这样,如果程序崩溃,这些值仍将保留。