title: How to save HashMap to a file and load it
body: <p>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.</p>
  
  <p>I know questions like this were asked like 100 before and I tried about 25 of them, but none of them worked.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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 <code>openFileOutput()</code> method.</p>
  
  <p>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.</p>
  
  <p>Some of the methods I used:</p>
  
  <pre><code>        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();
      }
  </code></pre>
  
  <p>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'...</p>
  
  <pre><code>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();
      }
  </code></pre>
  
  <p>This method doesn't work either, I don't really know which exception I got.</p>
  
  <p>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.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I tried to use the 'this.getApplicationContext().getFilesDir()' method but still getting a 'FileNotFoundException'... Don't know what to do.</p>