2017-10-09 59 views
-3

我想从.dat文件中检索多个hashmaps 但是它只给我第一个存储!反序列化HashMap dosent给我所有的元素

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 
 
{ 
 
\t public static void main(String args[]) throws IOException, ClassNotFoundException 
 
\t { 
 
\t \t HashMap<String, String> students = new HashMap<String, String>(); 
 

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

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

 

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

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

 
\t \t } 
 

 
\t } 
 

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

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

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

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

+2

适用于我(Linux,Java 8)。 – Robert

+2

您提供的代码无法在我们的机器上重现问题。你能重现吗?你真的只是使用这个代码?在保存之前,也许你还有其他一些类操作你的设置...因为除了代码中的一些缺陷之外,它看起来是正确的。 – Zabuza

+0

完美的作品 –

回答

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

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

删除true参数。

+0

但我需要每次更新hashmap并保存所有数据! – Sara

+0

每次覆盖文件都会完成该操作。即使它起作用,你也不需要追加,即使它没有。打电话给我们,当你尝试过。之前没有。 – EJP