简体   繁体   中英

How to save HashMap to a file and load it

I got a major problem. I want to save a HashMap (I'll call it 'map' or 'hm') to a file on the android device the application is running on. I do want to save it on the internal storage.

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

I got 3 classes, one main class which extends 'Activity' and two other classes, one called 'util'. I wanted to write two methods, to save and load the HashMap, and both of the methods should be in the 'util' class. In the main class I wrote several methods to load the HashMap, put something in it, and save it again.

Till here it shouldn't be difficult to solve my problem, but I want to use the 'save' and 'load' methods in other classes except of the main class too.

I don't know how to get the Context object in other classes as the main class, so I don't know how to call the openFileOutput() method.

In summary: I want to save and load a HashMap to / from a file which I want to create on the internal storage. The method I use should be located in the 'util' class and should be accessible for all other classes, preferred in the static way. I tried many different possibilities but I always get a 'FileNotFoundException'. I hope you can help me.

Some of the methods I used:

        try {
        FileOutputStream fos = c.openFileOutput(s, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(hm);
        oos.flush();
        oos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

At this method I have to use the Context, and I don't know how to get it in other classes. I also get a 'FileNotFoundException'...

String file = The path where my file should be located (I don't know how it should be given, but "data/data/[packagename]/[file]" does not work)

        try {
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(hm);
        oos.flush();
        oos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

This method doesn't work either, I don't really know which exception I got.

Finally: If you want to help me, please describe how to get each Object you use (Well, except of basic stuff like strings, etc...) and paste your whole solution. I'm really sorry if exactly this question was asked before, I couldn't find it.

Edit

I tried to use the 'this.getApplicationContext().getFilesDir()' method but still getting a 'FileNotFoundException'... Don't know what to do.

If you always call your Util -methods from classes with access to an Activity , just add the Context as a parameter in your method call.

Otherwise, you can create a global Application -object from which you can get a Context. This particular Context does not support creation of Views and other Activity-dependent options, but the openFileOutput() / openFileInput() -methods should be fine since the directory is on Application-level.

To do this you would create a subclass of Application with a static reference to itself:

//In the Application subclass:
static MyApplication instance;
onCreate(){
    super.onCreate();
    instance = this;
}
public static final MyApplication getInstance(){
    return instance;
}

Then you can just call MyApplication.getInstance(); to get a Context.

You will also have to add your application-class in the manifest (With the name-attribute under the application-tag).

Related reading: Singletons vs ApplicationContext in android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM