0

I have an android app that is writing a values to a file that the app also creates. I am able to write to the file and then again read from the file. However, as soon as that activity is finished, it seems that the file is now gone, or loses it's values.

I know you can't browse the files through explorer unless you root your phone and/or run the adb server as a specific user.

Here is my code for writing to the file: public void savePrices(View view) { FileOutputStream outputStream;

        File getFilesDir = this.getFilesDir();
        File filePathOne = new File(getFilesDir, filename);
        try {
            outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
            for (int i = 0; i < priceArray.length; i++) {
                outputStream.write(String.format("%.2f\n", priceArray[i]).getBytes());
            }

            Toast.makeText(this, "Prices saved successfully!", Toast.LENGTH_SHORT).show();
            outputStream.close();
            } catch (Exception e) {
            e.printStackTrace();
        }

Here is my code that reads the file:

public void loadPrices(View view) {
        int i = 0;
        final InputStream file;
        BufferedReader reader;
        try{
            file = getAssets().open(filename);
            reader = new BufferedReader(new InputStreamReader(file));
            String line = reader.readLine();
            while(line != null){
                line = reader.readLine();
                priceArray[i] = Double.parseDouble(line);
                i++;
            }
        } catch(IOException ioe){
        ioe.printStackTrace();

        hamburgerPriceText.setText(String.format("%.2f", priceArray[0]));
        hotDogPriceText.setText(String.format("%.2f", priceArray[1]));
        chipsPriceText.setText(String.format("%.2f", priceArray[2]));
        beerPriceText.setText(String.format("%.2f", priceArray[3]));
        popPriceText.setText(String.format("%.2f", priceArray[4]));

        Toast.makeText(this, "Prices loaded successfully!", Toast.LENGTH_SHORT).show();

        }catch (NumberFormatException e) {
            Log.e("Load File", "Could not parse file data: " + e.toString());
        }
    }

After I call the save method which sets the values in the array and saves the values to the file, I run a clear method that removes all the values on the activity fields and in the array. So when I run the read method and it populates the fields on the activity, I know the values are coming from reading the file. This is the only way that I know that I'm saving and reading from the file successfully.

My question is how do I make it permanent? If I close the activity that saves the values and then immediately run the read method, all the values are 0.

Is there something that I am missing? How can I write to a file so if the activity is closed, or the app is completely closed, I can still retain the values?

GIZNAJ
  • 501
  • 5
  • 23

1 Answers1

1

Here is my code that reads the file:

There is nothing in that code that reads a file. It is reading some stuff out of the your app's assets. Also, for some reason, it is only updating the UI if you have an exception.

So when I run the read method and it populates the fields on the activity, I know the values are coming from reading the file.

No, they are coming from your app's assets, and you are only populating the fields if you have an IOException.

My question is how do I make it permanent?

Step #1: Actually read from the file. Since you are using openFileOutput() to write to the file, use openFileInput() to read from the file.

Step #2: Update the UI when you successfully read in the data, not in the catch block for the IOException.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks @CommonsWare I actually do populate the fields in the GUI without exception. I can run Save, Clear and then Read without exceptions and see the values in the activity populate, disappear and come back. Is that what you mean? – GIZNAJ Aug 09 '15 at 22:32
  • @Aaron.Toth: Look at your `loadPrices()` method. You are not reading in your file (you are reading in an asset). And, you are only updating your widgets in the `catch(IOException ioe)` block -- you are not updating the widgets elsewhere in this method. – CommonsWare Aug 09 '15 at 22:47
  • I updated it just as you replied and I makes sense what you're saying: file = getAssets().open(filename); – GIZNAJ Aug 09 '15 at 22:53
  • I don't think I can show you my updated code in a comment – GIZNAJ Aug 09 '15 at 22:54
  • I think I'm close now. I just have a problem with my while loop in my read, but I get past that. – GIZNAJ Aug 09 '15 at 23:03