1

原始字符串有换行符。我将字符串保存在一个活动中并在另一个活动中检索它。

这是我用来写入内部存储的代码

try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
            outputStreamWriter.write(strText);
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        } 

这是我用来阅读的代码

InputStream inputStream = openFileInput(FILENAME);

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

            SomeArray.add(ret);

该代码唯一有效的问题是我丢失了换行符。

4

2 回答 2

3

我通过将while循环更改为

 while ( (receiveString = bufferedReader.readLine()) != null ) {

                        ret += receiveString + "\n";
                    }
于 2013-12-02T00:05:11.010 回答
0

readLine 去除尾行字符: http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#readLine()

使用 read 方法一次以合理的块读取文件并将这些块连接起来。将以这种方式保留结束线。

于 2013-12-02T00:01:41.443 回答