0

I am looking to save/load a very easy application in android. The thought would be checking if a file exists in the onCreate() method and if it does load the settings in onCreate. Is this the right function to do this in? Saving is done in the onPause() function.

The way I would be doing it is via FileOutputStream and FileInputStream.

But how does my code look then? Currently I have this:

if (new File(FILENAME).isFile()) {
        FileInputStream fis = null;
        try {
            fis = openFileInput(FILENAME);
            fis.read();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        numOfEpisode = Integer.parseInt(fis.toString());
        numOfSeason = Integer.parseInt(fis.toString());
    }

Ignore the fact that I haven't handled the exceptions yet, since I know the file will be there when testing.

protected void onPause() {
    try {
        FileOutputStream fos = openFileOutput(FILENAME,
                Context.MODE_PRIVATE);
        fos.write(numOfEpisode);
        fos.write(numOfSeason);
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The App currently gives me an error when I close the app(when onPause() is called). Could anyone point me in any direction to why?

Also how do I know in what way FileOutputStream.write() writes my statements? Is this FIFO or LIFO?

Adrian Jandl
  • 2,985
  • 4
  • 23
  • 30
  • 3
    Is there any reason you're using files for this? If you're only saving 2 `int`s I would just store them in [SharedPreferences](http://developer.android.com/reference/android/content/SharedPreferences.html) – James McCracken Feb 18 '13 at 18:55

2 Answers2

2

I would use SharedPreferences for this as it's much easier to do and is specifically designed for storing and retrieving small amounts of information from the device. Check out How to use SharedPreferences in Android to store, fetch and edit values to see how this is done.

You would read the values in onCreate and store them. When you onPause you would then write them out to SharedPreferences.

Community
  • 1
  • 1
Dororo
  • 3,420
  • 2
  • 30
  • 46
  • `SharedPreferences prefs = getPreferences(MODE_PRIVATE); numOfEpisode = prefs.getInt("numOfEpisode", 0); numOfSeason = prefs.getInt("numOfSeason", 0); ` For reading `SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); editor.putInt("numOfEpisode", numOfEpisode); editor.putInt("numOfSeason", numOfSeason); editor.commit();` For writing. The function works(It does read and write), however the error remains when I close my Application. – Adrian Jandl Feb 18 '13 at 20:01
  • You need to call super.onPause() e.g. `protected void onPause() { super.onPause(); // insert everything else here // }` – Dororo Feb 18 '13 at 21:06
  • Ofcourse! Can't believe I missed that. Thanks a lot. Working now – Adrian Jandl Feb 18 '13 at 21:12
0

Try it with this code:

File Gdir = new File(Environment.getExternalStorageDirectory()
                    .getPath() + "/NewFolder/");
            // have the object build the directory structure, if needed.

            if (!Gdir.exists()) {
                Gdir.mkdirs();
            }
                File outputFile = new File(Gdir, "file.txt");
                // now attach the OutputStream to the file object, instead of a
                // String representation
                try {                   
                    FileWriter mod = new FileWriter(
                            outputFile);
                    mod.write(numOfEpisode);
                    mod.write(numOfSeason);
                    mod.flush();
                    mod.close();

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Toast.makeText(getApplicationContext(), "File saved in: "+outputFile, Toast.LENGTH_LONG).show();
Rotary Heart
  • 1,899
  • 3
  • 29
  • 44