Sunday 15 July 2012

java - How to save HashMap to a file and load it -


I have a big problem. I want to save a hushmap (I would say it 'map' or 'hm') In a file that is running on the Android device. I want to save it to internal storage.

I know that such questions were asked before 100 and I tried about 25 of them, but none of them worked.

I got 3 classes, one main square which extends the 'activity' and two other classes, one is called 'Usage'. I want to write two methods, to save and load a hashmap, and both methods should be in the 'use' class. In the main category, I've written several ways to load a Hashmap, put something in it and saved it again.

Even my problem should not be difficult to solve, but I also have 'saving' and 'load' methods in other classes except the main class.

I do not know how to get a contextual item in the other classes as a main square, so I do not know how to call openFileOutput () method.

In short: I want to save and load a hashmap from / from a file, which I want to build on internal storage. The method I use should be located in the 'use' class and should be accessible to all other sections, which have been liked in a steady manner. I tried many different possibilities but I always get 'FileNotFoundException'. I hope you can help me.

Some of the methods I used:

  try {FileOutputStream fos = c.openFileOutput (reference, reference. MODE_PRIVATE); ObjectOutputStream OOS = New ObjectOutputstream (fos); Oos.writeObject (hm); Oos.flush (); Oos.close (); Fos.close (); } Hold (FileNotFoundException e) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); }   

On this method, I have to use references, and I do not know how to get it into other classes. I also get 'FileNotFoundException' ...

  string file = path where my file should be located (I do not know how this should be given, but "data / data / [Packagename] / [File] "does not work) {FileOutputStream fos = new FileOutputStream (file); ObjectOutputStream OOS = New ObjectOutputstream (fos); Oos.writeObject (hm); Oos.flush (); Oos.close (); Fos.close (); } Hold (FileNotFoundException e) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); }   

This method does not work either, I really do not know what exception I got.

Finally: If you want to help me, please describe how to get each object (well, except the basic stuff, strings etc ...) and your complete solution Paste I really regret that before the question was asked, I could not find it.

Edit

I still have a 'FileNotFoundException' on the 'GetFilesDir ()' method ... I do not know what to do.

If you always Util -methods to a activity from the reaching class, just add context as a parameter to your method call.

Otherwise, you can create a global object, from which you can get a reference. It does not support the creation of special reference scenes and other activity-based options, but openFileOutput () / openFileInput () - the method should be fine because the directory is on the application -Livel

To do this, you create a sub-section of the application for yourself with a constant reference:

  // In the application sub-class: Fixed MyApplication Example; OnCreate () {super.onCreate (); Example = this; } Public stable last MyApplication getInstance () {return example; }   

Then you simply get a code of MyApplication.getInstance (); You can call .

You will also need to add your application - in the display class (with name-attribute under the application-tag).

Related reading:

No comments:

Post a Comment