反序列化HashMap dosent给我所有的元素

时间:2017-10-09 01:48:58

标签: java hashmap

我想从.dat文件中检索多个哈希图,但它只给我存储的第一个哈希图!

public class TestSerialize
{ 
    public static void main(String args[]) throws IOException
    {
        HashMap<String, String> students = new HashMap<String, String>();

        // This part I changed everytime to keep the data stored in "students.dat"
        // I want to retrieve all the keys that have been run 
        students.put("22", "xxx");
        students.put("33", "yyy");

        ObjectOutputStream out = new ObjectOutputStream(
            new FileOutputStream("students.dat", true));
        out.writeObject(students);
        out.close();       
     }
}

以下代码用于反序列化

public class TestSerialize2
{
    public static void main(String args[]) throws IOException, ClassNotFoundException
    {
        ObjectInputStream in = new ObjectInputStream(
            new FileInputStream("students.dat"));
        HashMap<String, String> students = new HashMap<String, String>();

        students = (HashMap<String, String>) in.readObject();
        in.close();       

        for (String s : students.keySet())
            System.out.println(s);
    }
}

我希望很清楚......

(当你为学生提供不同的密钥时,第二次运行(TestSerialize类)时出现问题..(TestSerialize2类)的输出将是旧值,只有22,33)希望有帮助

发表评论后我试试这个:  但同样的问题仍然存在!!

package week8;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

public class TestSerialize
{
	public static void main(String args[]) throws IOException, ClassNotFoundException
	{
		HashMap<String, String> students = new HashMap<String, String>();

		// This part I changed everytime to keep the data stored in "students.dat"
		// I want to retrieve all the keys that have been run 
		students.put("66", "xxx");
		students.put("99", "yyy");

		ObjectOutputStream out = new ObjectOutputStream(
				new FileOutputStream("students.dat", true));


		File file = new File("students.dat");
		if (file.length() == 0){
			System.out.print("sss");
			out.writeObject(students);
			out.close();     
		}else
		{

			students =readThenWrite(students);
			for (String s : students.keySet())
	            System.out.println(s);
			
			out.writeObject(students);
			out.close();
			 
			
	        

		} 

	}

	private static HashMap<String, String> readThenWrite(HashMap<String, String> students) {
        HashMap<String, String> ss = new HashMap<String, String>();

        ObjectInputStream in;
		try {
			in = new ObjectInputStream(
			            new FileInputStream("students.dat"));
		

		        ss = (HashMap<String, String>) in.readObject();
		      
		        in.close();       

		        for (String s : students.keySet())
		            ss.put(s, students.get(s));
		        
		        
		        
		        
		} catch (IOException | ClassNotFoundException e) {
 			e.printStackTrace();
		}
		
		return ss;
	}
}

1 个答案:

答案 0 :(得分:0)

new FileOutputStream("students.dat", true));

问题出在这里。第二个参数表示您正在追加新数据,而不是覆盖它,但您的读取代码不会尝试从文件中读取多个映射。实际上,因为你不能附加到对象流,至少不能那样。

删除true参数。