使用 BufferedReader 时在新行拆分 Android

Split on new line when using BufferedReader Android

我正在尝试从文本文件中读取,在遇到换行符时拆分内容并将结果显示在 TextView 中。

这是我拥有的 readfromfile 方法:

public String readFromFile(Context context) {
        String ret = "";
        try {
            InputStream inputStream = context.openFileInput("history.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 ) {
                    //receiveString.split("\n");
                    //String[] split = receiveString.split("\n\s*");
                    stringBuilder.append(receiveString);
                   // if((receiveString = bufferedReader.readLine()) == "\n"){
                   // }
                }
                inputStream.close();
                //stringBuilder.
                ret = stringBuilder.toString();
            }
        }
        catch (FileNotFoundException e) {
            Log.e("login activity", "File not found: " + e.toString());
            Toast.makeText(this, "You have not sent any messages!!", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Log.e("login activity", "Can not read file: " + e.toString());
            Toast.makeText(this, "Cannot read from file!!", Toast.LENGTH_LONG).show();
        }
        return ret;
    }

这就是调用方法的地方:

String message = readFromFile(getApplicationContext()); // is this really needed? yes it is, use with readFromFile()
        TextView textView = new TextView(this);
        params2.addRule(RelativeLayout.BELOW, history.getId()); // this is very important for spacing using ids
        textView.setTextSize(14); //this might give an error or look weird on devices, horizontally and vertically
        textView.setText(message); // simplest fix is to force device to stick with horizontal, no common device is bigger than 5.5"
        //textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        textView.setId(View.generateViewId()); // This requires a minimum API 17
        textView.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        layout.addView(textView, params2);

我认为问题可能出在我与要显示的字符串一起使用的 append,即我正在返回一个带有附加文本的字符串。

这是视图输出:

Output

我正在尝试从名为 history.txt 的文件加载,该文件包含使用该应用程序以字符串格式发送的所有短信。

这是 writeToFile 方法:

public void writeToFile(String data,Context context) {
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("history.txt", Context.MODE_APPEND));
            //format data string here to include time stamp
            BufferedWriter writer = new BufferedWriter(outputStreamWriter);

            String currentDateTime = DateFormat.getDateTimeInstance().format(new Date());

                outputStreamWriter.append("\n");
                outputStreamWriter.append(data);
                outputStreamWriter.append(" ");
                outputStreamWriter.append(currentDateTime);
                writer.newLine();

                outputStreamWriter.close();

        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        }
    }

有什么想法吗?

检查 Javadoc。 readLine() 删除行终止符。如果你在输出时想要行,你必须自己添加行终止符:在这种情况下,在你阅读它时向每个 like 添加 \n\r\n ,因为你正在显示数据图形用户界面。

也许您应该改用 ListView。 ListView 的每一行都可以是文件中一行的单个 TextView,或者如果您想将数据分成格式良好的列,则可以是多个 TextView。

这已经通过添加 if 条件解决了。由于变量的数量已设置且不会更改,因此在每 8 行读取时,将跳过两行,如下所示:

 while ( (receiveString = bufferedReader.readLine()) != null ) {
                    //receiveString.split("\n");
                    //String[] split = receiveString.split("\n\s*");
                    stringBuilder.append(receiveString);
                    if(i == 8) {
                        stringBuilder.append("\n");
                        i = 0;
                    }
                    stringBuilder.append("\n");
                   // if((receiveString = bufferedReader.readLine()) == "\n"){
                   i++;
                   // }
                }

感谢大家的参与!

p.s 正如@Code-Apprentice 所指出的,ListView 应该是从一开始就走的路。