Skip to main content

ObjectOutputStream and ObjectInputStream not working

No replies
tejisingh74
Offline
Joined: 2010-07-25
Points: 0

hi
i am trying to create a hashmap and enter entries in it by entering from command line and then storing the hashmap in a file and retrieving it later and continue adding further entries.however the hashmap is not being retrieved from the file.
the following is the code.

import java.util.*;
import java.io.*;
class oo
{
public static void main(String ss[]) throws Exception
{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("op.txt"));
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("op.txt"));
HashMap hm=null;

try
{
hm=(HashMap)ois.readObject();
}
catch(Exception ep) { System.out.println("no hashmap found");}
if(hm==null)
{
System.out.println("it is null");
hm=new HashMap();
}
else
{
System.out.println("it is not null");
hm.put(s[0],s[1]);
}
System.out.println(hm);
ois.close();

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