写入文件,但在

时间:2018-02-01 03:23:31

标签: android file-io internal-storage

我正在为android中的内部存储写一个文本文件以保存一些设置。我相信写入文件功能正常工作,但是当我重新读取文件时(甚至直接写入后)文件为空。我已经在Stack溢出时经历了其他几个问题/答案,但没有一个能解决我的问题。

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();
        }

以下代码直接在上面的代码之后运行。但while ((line = br.readLine()) != null)始终为null,我认为这意味着文本文件为空。

        //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();
        }

    }

关于出了什么问题的任何想法?

1 个答案:

答案 0 :(得分:0)

写入文件

private void writeToFile(String data,Context context) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    } 
}

private String readFromFile(Context context) {

    String ret = "";

    try {
        InputStream inputStream = context.openFileInput("config.txt");

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append(receiveString);
            }

            inputStream.close();
            ret = stringBuilder.toString();
        }
    }
    catch (FileNotFoundException e) {
        Log.e("login activity", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("login activity", "Can not read file: " + e.toString());
    }

    return ret;
}