如何读取和写入一个HashMap文件? [英] How to read and write a HashMap to a file?

查看:279
本文介绍了如何读取和写入一个HashMap文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有以下的 HashMap

  HashMap< String,Object> fileObj = new HashMap< String,Object>(); 

ArrayList< String> cols = new ArrayList< String>();
cols.add(a);
cols.add(b);
cols.add(c);
fileObj.put(mylist,cols);

我把它写到一个文件如下:

 档案档案=新档案(temp); 
FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(fileObj);
s.flush();

现在我想将这个文件读回到Object是ArrayList的HashMap中。
如果我简单地做:

  File file = new File(temp); 
FileInputStream f = new FileInputStream(file);
ObjectInputStream s = new ObjectInputStream(f);
fileObj =(HashMap< String,Object>)s.readObject();
s.close();

这不会给我保存格式的对象。
它资料的预范读方信息mylist,[a,b,c]>对在第三个元素。我希望它只返回一个元素与我提供给它的价值第一。

//我如何读取同一个对象回到一个HashMap?

OK所以根据Cem的说明:这是什么似乎是正确的解释:
$ b

ObjectOutputStream序列化对象(HashMap in this大小写)ObjectInputStream将理解反序列化的任何格式,对于任何Serializable对象来说都是这样。
如果你想以你想要的格式进行序列化,你应该编写你自己的序列化器/反序列化器。

$ b

:当我从文件中读取Object并获取数据并执行所需的任何操作时,我只是遍历HashMap中的每个元素。 (它只在有数据的地方进入循环)。

感谢,

  public static void main(String ... args)
抛出IOException,ClassNotFoundException {
HashMap< String,Object> fileObj = new HashMap< String,Object>();

ArrayList< String> cols = new ArrayList< String>();预范信息亦亦范读亦范辛
cols.add(b);
cols.add(c);
fileObj.put(mylist,cols);
{
File file = new File(temp);
FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(fileObj);
s.close();
}
档案档案=新档案(temp);
FileInputStream f = new FileInputStream(file);
ObjectInputStream s = new ObjectInputStream(f);
HashMap< String,Object> fileObj2 =(HashMap< String,Object>)s.readObject();
s.close();

Assert.assertEquals(fileObj.hashCode(),fileObj2.hashCode());
Assert.assertEquals(fileObj.toString(),fileObj2.toString());
Assert.assertTrue(fileObj.equals(fileObj2));
}


I have the following HashMap:

HashMap<String,Object> fileObj = new HashMap<String,Object>();

ArrayList<String> cols = new ArrayList<String>();  
cols.add("a");  
cols.add("b");  
cols.add("c");  
fileObj.put("mylist",cols);  

I write it to a file as follows:

File file = new File("temp");  
FileOutputStream f = new FileOutputStream(file);  
ObjectOutputStream s = new ObjectOutputStream(f);          
s.writeObject(fileObj);
s.flush();

Now I want to read this file back to a HashMap where the Object is an ArrayList. If i simply do:

File file = new File("temp");  
FileInputStream f = new FileInputStream(file);  
ObjectInputStream s = new ObjectInputStream(f);  
fileObj = (HashMap<String,Object>)s.readObject();         
s.close();

This does not give me the object in the format that I saved it in. It returns a table with 15 null elements and the < mylist,[a,b,c] > pair at the 3rd element. I want it to return only one element with the values I had provided to it in the first place.
//How can I read the same object back into a HashMap ?

OK So based on Cem's note: This is what seems to be the correct explanation:

ObjectOutputStream serializes the objects (HashMap in this case) in whatever format that ObjectInputStream will understand to deserialize and does so generically for any Serializable object. If you want it to serialize in the format that you desire you should write your own serializer/deserializer.

In my case: I simply iterate through each of those elements in the HashMap when I read the Object back from the file and get the data and do whatever I want with it. (it enters the loop only at the point where there is data).

Thanks,

解决方案

You appear to be confusing the internal resprentation of a HashMap with how the HashMap behaves. The collections are the same. Here is a simple test to prove it to you.

public static void main(String... args)
                            throws IOException, ClassNotFoundException {
    HashMap<String, Object> fileObj = new HashMap<String, Object>();

    ArrayList<String> cols = new ArrayList<String>();
    cols.add("a");
    cols.add("b");
    cols.add("c");
    fileObj.put("mylist", cols);
    {
        File file = new File("temp");
        FileOutputStream f = new FileOutputStream(file);
        ObjectOutputStream s = new ObjectOutputStream(f);
        s.writeObject(fileObj);
        s.close();
    }
    File file = new File("temp");
    FileInputStream f = new FileInputStream(file);
    ObjectInputStream s = new ObjectInputStream(f);
    HashMap<String, Object> fileObj2 = (HashMap<String, Object>) s.readObject();
    s.close();

    Assert.assertEquals(fileObj.hashCode(), fileObj2.hashCode());
    Assert.assertEquals(fileObj.toString(), fileObj2.toString());
    Assert.assertTrue(fileObj.equals(fileObj2));
}

这篇关于如何读取和写入一个HashMap文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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