1

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 ?

Abhishek Jain
  • 4,408
  • 7
  • 31
  • 49
hudi
  • 14,603
  • 43
  • 135
  • 237

4 Answers4

4

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

Jeff Foster
  • 42,070
  • 11
  • 81
  • 103
0

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.

Pleasant94
  • 450
  • 1
  • 7
  • 20
Azodious
  • 13,563
  • 1
  • 33
  • 68
0

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

Bhavik Shah
  • 5,035
  • 2
  • 22
  • 39
-1

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

mthmulders
  • 9,284
  • 4
  • 34
  • 53