如何在输入流中保存新行? Java - Android

How to save a new line in input stream? Java - Android

我想使用来自 EditText 的 InputStream 保存文本,同时保留文本中的新行。

这是我正在尝试做的示例:

    public void onSaveButtonClick(View v) {
    try {
        OutputStreamWriter out= new OutputStreamWriter(openFileOutput("STORETEXTBELESKE.txt", 0));

        out.write(beleskeEditText.getText().toString());

        out.close();
    }

    catch (Throwable t) {

        Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();

    }
}

下面是从保存的文件中读取文本的部分:

    public void readSavedFile() {
    try {
        InputStream in = openFileInput("STORETEXTBELESKE.txt");

        if (in != null) {

            InputStreamReader tmp = new InputStreamReader(in);
            BufferedReader reader = new BufferedReader(tmp);
            String str;
            StringBuilder buf = new StringBuilder();

            while ((str = reader.readLine()) != null) {

                buf.append(str);

            }
            in.close();
            beleskeEditText.setText(buf.toString());
        }
    } catch (java.io.FileNotFoundException e) {

    } catch (Throwable t) {
        Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
    }
}

它部分有效。所有文本都已正确保存,但所有内容都在同一行中。

所以如果输入是:

测试 测试 123

输出为:Testtest123

就这样做

while ((str = reader.readLine()) != null) {
      buf.append(str);
      buf.append('\n');
}