1

I wrote this function which will attempt to store the map but its not working I think? I am using netbeans and each time i go to the directory of project in the java src i can't find the created file or anywhere else in the project. The map is surely valid because output comes out perfect when am not dealing with storage. btw I do implement seriliazable :)

Note: the map is of type TreeMap

 public boolean storeMap(TreeMap<DateTime, Integer> map){
   try{
     f_out = new FileOutputStream("mapObject.data");
     obj_out = new ObjectOutputStream (f_out);
     obj_out.writeObject(map);
     return true;
    }catch(IOException ioe){
        System.err.print(ioe);
        return false;
    }
}

is there a reason why the output file is not generated? Thanks

6
  • Try flushing the output and closing it. Commented Nov 22, 2011 at 10:51
  • 1
    You should always close your streams, add 'f_out.close();' before your 'return true'. Commented Nov 22, 2011 at 10:52
  • I just did and nothing in src folder changed Commented Nov 22, 2011 at 10:55
  • Even if it did not solve that problem, keep the 'close()' it might well save you trouble later. Leaving streams open can cause headaches. Commented Nov 22, 2011 at 10:57
  • 1
    Oh, by the way, the file will be generated in the folder where you run your program, if you use eclipse it will be the project folder. It will not be the folder where the source file is located Commented Nov 22, 2011 at 10:58

3 Answers 3

1

I suggest to use absolute path, that is something like

f_out = new FileOutputStream("/home/username/mapObject.data");

or on windows

f_out = new FileOutputStream("c:\\work\\mapObject.data");

If there was no exception thrown (System.err.print(ioe); this line did not print anything) then the file was created somewhere.

Sign up to request clarification or add additional context in comments.

1 Comment

perfect! that did it! I thought it would just assume the current working directory and store there :) appreciate it
1

SERIALIZE A HASHMAP: This code is working fine , I have implemented and used in my app. Plz make ur functions accordingly for saving map and retrieving map.

Imp thing is, you need to make confirm that the objects you are putting as value in map must be serializable , means they should implement serailizbele interface. ex. Map<.String,String> hashmap=new HashMap<.String,String>().. here in this line ...map and string both are implictly serializable , so we dont need to implement serializble for these explicitly but if you put your own object that must be serializable.


public static void main(String arr[])
{
  Map<String,String> hashmap=new HashMap<String,String>();
 hashmap.put("key1","value1");
    hashmap.put("key2","value2");
    hashmap.put("key3","value3");
    hashmap.put("key4","value4");

     FileOutputStream fos;
    try {
        fos = new FileOutputStream("c://list.ser");

    ObjectOutputStream oos = new ObjectOutputStream(fos);
     oos.writeObject(hashmap);
     oos.close();

     FileInputStream fis = new FileInputStream("c://list.ser");
     ObjectInputStream ois = new ObjectInputStream(fis);
    Map<String,String> anotherList = (Map<String,String>) ois.readObject();

     ois.close();

     System.out.println(anotherList);

 } catch (FileNotFoundException e) {e.printStackTrace();
 } catch (IOException e) {e.printStackTrace();
    } catch (ClassNotFoundException e) {e.printStackTrace();
    }

}

Comments

0

Try to call fulsh() method for outputStreams.

    obj_out.flush();
    f_out.flush();

and close them in finally statment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.