简体   繁体   English

如何从文件中读取hashMap

[英]How to read hashMap from file

I found the code for reading a hashMap from file (on disk): 我找到了从文件(在磁盘上)读取hashMap的代码:

public HashMap<String, Integer> load(String path)
{
    try
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
        Object result = ois.readObject();
        //you can feel free to cast result to HashMap<String, Integer> if you know that only a HashMap is stored in the file
        return (HashMap<String, Integer>)result;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

but I did not find any example how does this file look like. 但我没有找到任何示例这个文件是怎么样的。 Can you explain this with the help of an example ? 你能借一个例子解释一下吗?

您需要使用ObjectOutputStream将其写出来(请参阅此处的文档)。

The file in question is nothing but a Serialized HashMap. 有问题的文件只是一个序列化的HashMap。

If you wanna see it, first Serialize a HashMap and find it out. 如果你想看到它,首先序列化一个HashMap并找到它。

To Serialize, you can use following code: 要序列化,您可以使用以下代码:

HashMap<String,Integer> aHashMap = new HashMap<String,Integer>();  
aHashMap.put("one",1);   
aHashMap.put("two",2);
aHashMap.put("three",3);

File file = new File("serialized_hashmap");   
FileOutputStream fos = new FileOutputStream(file);   
ObjectOutputStream oos = new ObjectOutputStream(fos);           
oos.writeObject(aHashMap); 
oos.flush();

Now, this file you can use with the program you provided. 现在,您可以将此file与您提供的程序一起使用。

This is a typical problem of serializing the objects 这是序列化对象的典型问题

    import java.io.*; 
    public class SerializationDemo { 
    public static void main(String args[]) { 
    // Object serialization 
    try { 
    MyClass object1 = new MyClass("Hello", -7, 2.7e10); 
    System.out.println("object1: " + object1); 
    FileOutputStream fos = new FileOutputStream("serial"); 
    ObjectOutputStream oos = new ObjectOutputStream(fos); 
    oos.writeObject(object1); 
    oos.flush(); 
    oos.close(); 
    } 
    catch(Exception e) { 
    System.out.println("Exception during serialization: " + e); 
    System.exit(0); 
    }
}

of course the MyClass should implement serializable interface 当然,MyClass应该实现可序列化的接口

class MyClass implements Serializable { 
String s; 
int i; 
double d; 
public MyClass(String s, int i, double d) { 
this.s = s; 
this.i = i; 
this.d = d; 
} 
public String toString() { 
return "s=" + s + "; i=" + i + "; d=" + d; 
} 
}

do this and see the file 这样做,看到文件

This example uses Serialization , a technique to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. 此示例使用Serialization ,这是一种将其状态转换为字节流的技术,以便可以将字节流还原为对象的副本。

So, the way to create such a file would be to serialize a HashMap<String, Integer> first, like this: 因此,创建这样一个文件的方法是首先序列化HashMap<String, Integer> ,如下所示:

public void serializeHashMap(HashMap<String, Integer> m) {
    FileOutputStream fos = new FileOutputStream("hashmap.dat");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    oos.writeObject(m);
    oos.close();
}

(This doesn't cover Exception handling and stuff, but you get the idea...) (这不包括异常处理和东西,但你明白了......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM