如何序列化和反序列化哈希表?

我正在尝试但Serialize没有成功。DeserializeHashtable


这是代码:


反序列化


    public static void read(File f) throws IOException, ClassNotFoundException

    {

        FileInputStream fos = new FileInputStream(f);

        ObjectInputStream oos = new ObjectInputStream(fos);


        list = new Hashtable<Date,String>((Hashtable<Date,String>)oos.readObject());

        oos.close();

    }

连载


    public static void write(String s) throws FileNotFoundException, IOException

    {

        FileOutputStream fos = new FileOutputStream(s);

        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(list);

    }

我写了Class日期,它不是 Java 的,但我在implemet Serializable那里写了。


在我打印它之后,我得到Deserialize了.Hashtable{}


我究竟做错了什么?


森栏
浏览 130回答 4
4回答

慕的地8271018

在我反序列化Hashtable并打印它之后,我得到了{}.我究竟做错了什么?此代码是从您的 MCVE 代码复制而来的。您已经将其编辑掉了,但我相信这个或类似的东西是您问题的真正原因。public static void write(String s) throws FileNotFoundException, IOException{&nbsp; &nbsp; list = new Hashtable<Date,String>();&nbsp; &nbsp; FileOutputStream fos = new FileOutputStream(s);&nbsp; &nbsp; ObjectOutputStream oos = new ObjectOutputStream(fos);&nbsp; &nbsp; oos.writeObject(list);}您应该调用close(),oos但这不是真正的问题。(丢失close() 可能会导致序列化文件为空或不完整/损坏,但这不会导致您读回空Hashtable的 .new FileOutputStream(s)真正的问题是方法的第一条语句。您无条件地将一个新的空值分配Hashtable给list. 然后你在写它。所以(自然地)当你读回你写入文件的内容时,你会Hashtable再次得到一个空的。这就是{}你所看到的意思。简而言之,您的代码正在写出一个空表并再次读回。Hashtable序列化正在按预期工作。

GCT1015

我相信你应该在写完后冲水。oos.writeObject(列表);&nbsp;oos.close();

偶然的你

您是否正在向文件写入任何内容,因为 write 方法将 String 作为参数,但您尚未指定要写入哪个文件。尝试:public static void write(File f) throws FileNotFoundException, IOException{&nbsp; &nbsp; FileOutputStream fos = new FileOutputStream(f);&nbsp; &nbsp; ObjectOutputStream oos = new ObjectOutputStream(fos);&nbsp; &nbsp; Hashtable<Date, String> list = new Hashtable<>();&nbsp; &nbsp; list.put(YourDateObject, "String");&nbsp; &nbsp; oos.writeObject(list);}

德玛西亚99

这是代码示例:public class Main {/**&nbsp;* File name&nbsp;*/private final static String FILENAME = "test.bin";/**&nbsp;* Entry point&nbsp;*&nbsp;&nbsp;* @param args&nbsp;* @throws Exception&nbsp;&nbsp;*/public static void main(String[] args) throws Exception {&nbsp; &nbsp; Hashtable<Date, String> original = new Hashtable<>();&nbsp; &nbsp; // write some data to hashtable&nbsp; &nbsp; for (int i = 0; i < 3; i ++) {&nbsp; &nbsp; &nbsp; &nbsp; original.put(new Date(), String.valueOf(i));&nbsp; &nbsp; &nbsp; &nbsp; TimeUnit.MILLISECONDS.sleep(100);&nbsp; &nbsp; }&nbsp; &nbsp; // serialize&nbsp; &nbsp; write(FILENAME, original);&nbsp; &nbsp; // deserialize&nbsp; &nbsp; Hashtable<Date, String> restored = read(FILENAME);&nbsp; &nbsp; // compare results&nbsp; &nbsp; System.out.println(restored);&nbsp; &nbsp; System.out.println(restored);}/**&nbsp;* Deserialization&nbsp;*&nbsp;&nbsp;* @param filename&nbsp;* @return&nbsp;* @throws IOException&nbsp;* @throws ClassNotFoundException&nbsp;&nbsp;*/public static Hashtable<Date, String> read(String filename) throws IOException, ClassNotFoundException {&nbsp; &nbsp; try (ObjectInputStream oos = new ObjectInputStream(new FileInputStream(filename))) {&nbsp; &nbsp; &nbsp; &nbsp; return (Hashtable<Date, String>) oos.readObject();&nbsp; &nbsp; }}/**&nbsp;* Serialization&nbsp;*&nbsp;&nbsp;* @param filename&nbsp;* @param list&nbsp;* @throws FileNotFoundException&nbsp;* @throws IOException&nbsp;&nbsp;*/public static void write(String filename, Hashtable<Date, String> list) throws FileNotFoundException, IOException {&nbsp; &nbsp; try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {&nbsp; &nbsp; &nbsp; &nbsp; oos.writeObject(list);&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java