1

I'm writing and reading from a file and it works perfectly fine. However when the activity is switched or the app is closed it appears that the file is deleted or some such as null is returned when trying to read from the file. I believed it may be because I have an onCreate blank write to the file but that should only run upon the launch to make sure the file is created. I don't mind if the file doesn't persist between launches however it should at least persist between activities.

//in oncreate is writeToHistory("");

public void writeToHistory(String toWrite) {
        try {
            File path = getApplicationContext().getFilesDir();
            File file = new File(path, "JWCalcHistory.txt");
            FileOutputStream fos = new FileOutputStream(file);

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

            bw.write(toWrite);

            bw.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

public void btnAnsClicked(View v) throws IOException {
        File path = getApplicationContext().getFilesDir();
        File file = new File(path,"JWCalcHistory.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String oldAns = br.readLine();
        if (!(oldAns.equals("null") || oldAns.equals(""))) {
            if (Character.isDigit(readableSum.charAt(readableSum.length() - 1))) {
                oldAns = "*" + oldAns;
            }
            UpdateSum(oldAns);
        }
    }

If someone can point out a way to make the contents of the file persist always until it is programmatically deleted or cleared then please let me know. The file doesn't already exist and is created upon the code being run.

Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
  • 1
    `I believed it may be because I have an oncreate blank write to the file but that should only run upon the launch to make sure the file is created.` you were on your way to solving your own problem, why not add a check to see if the file exists first, if it does then you surely don't want to empty it out, right ? – a_local_nobody Apr 04 '22 at 10:54
  • Thanks for your suggestion, I discarded the idea that this may have been the issue seeing as how the oncreate should only run once, so while I'm currently solving the problem I cannot see why it became a problem. – jake.ward2639 Apr 04 '22 at 11:05
  • 1
    and you are sure it only runs once ? add logs to verify this – a_local_nobody Apr 04 '22 at 11:06
  • what i mean is that i was taught at my university that the oncreate method will only run once however that is clearly not the case. Honestly thanks because I was about to abandon my answer. – jake.ward2639 Apr 04 '22 at 11:12
  • You have learned right @jake.ward2639. When the app is opened from when it was destroyed the `onCreate` is re-executed. But, it does not reopen from when the app was in the recents. You can check my answer below – Sambhav Khandelwal Apr 04 '22 at 13:44

1 Answers1

0

You need to check if the file exists first. Something like this:

public void writeToHistory(String toWrite) {
        try {
            File path = getApplicationContext().getFilesDir();
            File file = new File(path, "JWCalcHistory.txt");
            if(file.exists()) return;
            FileOutputStream fos = new FileOutputStream(file);

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

            bw.write(toWrite);

            bw.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38