0

I am writing a text file to internal storage in android to save some settings. I believe that the write to file function is working properly, but when I re-read in the file (even directly after writing) the file is empty. I have gone through several other questions/answers on Stack overflow and none of them are solving my problem.

private void saveOtherData() {
        JSONObject settingsJson = new JSONObject();

        try {
            OutputStreamWriter writer;
            File testFile = new File(userProfile.this.getFilesDir(), "settings.txt");
            if (!testFile.exists()) {
                testFile.createNewFile();
                testFile.mkdir();
            }
            writer = new OutputStreamWriter(userProfile.this.openFileOutput("settings.txt", Context.MODE_PRIVATE));

            --code that adds a bunch of data to settingsJson

            writer.write(settingsJson.toString());
            //this line shows that settingsJson has the values I want
            System.out.println(settingsJson);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException ex) {
            ex.printStackTrace();
        }

The following code is run directly after the above code. But while ((line = br.readLine()) != null) is always null, which I assume means that the text file is empty.

        //test the file reader

        try {
            JSONObject settings;
            String jsonCode = "";
            StringBuilder text = new StringBuilder();
                File file = new File(userProfile.this.getFilesDir(), "settings.txt");

                BufferedReader br = new BufferedReader(new FileReader(file));
                String line;
                while ((line = br.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                }
                br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

Any ideas on what is going wrong?

chargerstriker
  • 486
  • 1
  • 5
  • 20
  • Well, your `mkdir()` makes no sense, because you're calling it after already creating the file. You never close the file after writing it, and the code you say *shows that settingsJson has what I want*, but that has nothing to do with the file. You have absolutely no app on your device that will allow you to open the `settings.txt` file to see if it's correct outside your app instead of just *believing* that you write it correctly? – Ken White Feb 01 '18 at 03:30
  • Is JSONObject settingsJson = new JSONObject(); Have some value? – lib4backer Feb 01 '18 at 03:34
  • Also, your `println` prints `settingsJson`, but your writing code writes `settingsJson.ToString()`. They're not the same thing. – Ken White Feb 01 '18 at 03:38
  • @KenWhite I just checked and printing `settingsJson.toString()` and just `settingsJson` print the same thing.... – chargerstriker Feb 01 '18 at 04:32
  • Wrong comparison. Println() may have overloaded functions. – greenapps Feb 01 '18 at 09:57
  • `writer.close();` is missing. – greenapps Feb 01 '18 at 09:58
  • `File testFile = new File(userProfile.this.getFilesDir(), "settings.txt"); if (!testFile.exists()) { testFile.createNewFile(); testFile.mkdir();`. Remove all that code. You dont need it. Moreove you try to create a directory where you already created a file with the same name. – greenapps Feb 01 '18 at 10:01
  • You should use `getFilesDir()` in both functions. – greenapps Feb 01 '18 at 10:03

0 Answers0