hudi
hudi

Reputation: 16525

How to read hashMap from file

I found the code for reading a hashMap from file (on disk):

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 ?

Upvotes: 1

Views: 7497

Answers (4)

Azodious
Azodious

Reputation: 13872

The file in question is nothing but a Serialized HashMap.

If you wanna see it, first Serialize a HashMap and find it out.

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.

Upvotes: 0

Bhavik Shah
Bhavik Shah

Reputation: 5183

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

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

Upvotes: 0

mthmulders
mthmulders

Reputation: 9705

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.

So, the way to create such a file would be to serialize a HashMap<String, Integer> first, like this:

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...)

Upvotes: -1

Jeff Foster
Jeff Foster

Reputation: 44706

You'd need to write it out using ObjectOutputStream (see the documentation here).

Upvotes: 4

Related Questions