我们可以将Hashtable写入文件吗? [英] Can we write a Hashtable to a file?

查看:495
本文介绍了我们可以将Hashtable写入文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

解决方案

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

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

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

然后你可以用 ObjectInputStream <来读它/ p>

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






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

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


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

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

解决方案

Yes, using binary serialization (ObjectOutputStream):

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

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

Then you can read it using ObjectInputStream

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


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

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

这篇关于我们可以将Hashtable写入文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆