0

All I would like to do is save and update a simple int[] with 20 highscores from an app I am making, but I keep getting fileNotFoundExceptions. I read: https://developer.android.com/guide/topics/data/data-storage.html#filesExternal, but it was not incredibly specific.

Used fields:

    private int[] highscores; //initialized as null in onCreate()
    private double dynamicHighScore; //calculated from intent that fires activity

code:

    String fileName = "highscores";



    try {
        FileInputStream fis = new FileInputStream(fileName);
        ObjectInputStream ois = new ObjectInputStream(fis); //firing fileNotFoundException everytime
        this.highScores = (int[]) ois.readObject();
        Log.d("GameOver", "Object Read");
        ois.close();
        fis.close();


        if (this.highScores.length < 20) {
            int[] newHighScores = new int[this.highScores.length + 1];
            newHighScores[newHighScores.length - 1] = (int) this.dynamicScore;
            this.highScores = newHighScores;
        } else if (this.highScores[this.highScores.length - 1] < this.dynamicScore) {
            this.highScores[this.highScores.length - 1] = (int) this.dynamicScore;
        }

        int j;
        int key;
        int i;

        // insertion Sort

        for (j = 1; j < highScores.length; j++) {
            key = highScores[j];
            for (i = j - 1; (i >= 0) && (highScores[i] < key); i--) {
                highScores[i + 1] = highScores[i];
            }
            highScores[i + 1] = key;
        }

        FileOutputStream fos = openFileOutput(fileName,
                Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(highScores);
        Log.d("GameOver", "Object rewritten");
                    oos.close();
        fos.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.d("fnf", "fnf1");
    } catch (IOException e) {
        e.printStackTrace();
        Log.d("IO", "IO1");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        Log.d("CNF", "CNF1");
    }

    if (this.highScores == null) {
        try {
            this.highScores = new int[] { (int) this.dynamicScore };
            FileOutputStream fos = openFileOutput(fileName,
                    Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(highScores);
            Log.d("GameOver", "Object created and written");
            oos.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }
    }

Upon GameOver after subsequent runs I cannot ever get the file to be found I have tried passing a File Object instead of the String fileName to new FileInputStream(), but no change My Logcat indicates that the Object is being written to memory, but not found from memory.

Does the file path have to be specified more explicitly, if so, where?

If the file path has to specified more explicitly, why is my Logcat logging successful object creation and file writing?

this int[] is the only object I wish to save to and access from memory; any help is much appreciated

Kdawg
  • 167
  • 11

1 Answers1

0

As pointed out by Simon's comment, there is probably a better way ( like SharedPreferences ), but to answer your question:

  1. You should check that the file exists before trying to open it. For example, something like (edited, previous example was broken ):

    if(Arrays.asList(fileList()).contains(fileName))

  2. Since you are writing the files with Context#openFileOutput you should open the FileInputStream with Context#openFileInput instead of new FileInputStream()

Additional issue with the code not directly related to the question:

  • The .close() should be in a finally, something like:

    finally{ try{ if( ois != null ){ ois.close(); } } catch ( ....

yogurtearl
  • 3,035
  • 18
  • 24