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
Can we write a Hashtable to a file?
提问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.
该教程也很有教育意义。

