解决方法
是的,使用二进制序列化(
ObjectOutputStream
):
FileOutputStream fos = new FileOutputStream("t.tmp"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(yourHashTable); oos.close();
然后可以使用ObjectInputStream读取它
你放在Hashtable(或更好的 – HashMap)中的对象必须实现Serializable
如果要以可读取的格式存储Hashtable,可以使用java.beans.XMLEncoder:
FileOutputStream fos = new FileOutputStream("tmp.xml"); XMLEncoder e = new XMLEncoder(fos); e.writeObject(yourHashTable); e.close();