【问题标题】:Retaining new line and other formatting from a .txt file in Android从 Android 中的 .txt 文件中保留新行和其他格式
【发布时间】:2015-09-28 23:32:25
【问题描述】:

好的,我正在开发我的第一个 Android 应用程序。如果这听起来很天真,请原谅我,因为我对此有点陌生,但我使用以下代码从 6 个不同的 .txt 文件读取到一个

private void displayFiles(int path) {
    TextView text = (TextView) findViewById(R.id.text);
    ImageView display = (ImageView) findViewById(R.id.head_image);
    String line = "";
    StringBuilder finalString = new StringBuilder();
    InputStream iStream = null;

    if (path == 0) {
        iStream = getResources().openRawResource(R.raw.salm);
        display.setImageResource(R.drawable.salm);
    } else if (path == 1) {
        iStream = getResources().openRawResource(R.raw.lyme);
        display.setImageResource(R.drawable.lyme);
    } else if (path == 2) {
        iStream = getResources().openRawResource(R.raw.pox);
        display.setImageResource(R.drawable.pox);
    } else if (path == 3) {
        iStream = getResources().openRawResource(R.raw.shig);
        display.setImageResource(R.drawable.shig);
    } else if (path == 4) {
        iStream = getResources().openRawResource(R.raw.gia);
        display.setImageResource(R.drawable.gia);
    } else if (path == 5) {
        iStream = getResources().openRawResource(R.raw.intro);
        display.setImageResource(R.drawable.intro);
    } else {
        Toast.makeText(this, "Something went wrong! Please refresh EpiQuick.", Toast.LENGTH_SHORT).show();
        return;
    }

    BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
    try {
        while ((line = bReader.readLine()) != null) {
            finalString.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    text.setText(finalString);
}

对应的xml:

<TextView
        android:id="@+id/text"
        style="@style/body"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/head_image"
        android:layout_marginBottom="16dp"
        />


  <style name="body">
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">#000000</item>
</style>

文本示例如下:

有风险的人: 前往贾第虫病常见国家的旅行者 托儿所中的人 与患病者有密切接触的人 吞下受污染饮用水的人 从湖泊或河流中饮用未经处理的水的背包客或露营者 与患病动物接触的人 男男性行为者

但是,当我在调用后运行应用程序时,所有换行符都会消失。我想知道是否有办法解决这个问题?如果有怎么办?

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    BufferedReader 的 readLine 方法会跳过行终止字符 (docs)。由于您只是将其显示到 TextView,因此您只需手动附加换行符:

        BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
        try {
            while ((line = bReader.readLine()) != null) {
                finalString.append(line);
                finalString.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    注意:不确定它是否在代码的某些部分,但请确保在使用后关闭 bReader

    【讨论】:

    • 我应该在哪里关闭 bReader?只要应用程序正在运行,它就会一直使用。
    • 这确实在其他方面有所帮助。谢谢。
    • 你可以将finally 阻塞到你的try-catch,然后调用bReader.close()。
    • 我不打算使用文件 I/O。 finally 调用应该在 catch 块之后还是在它内部?当我尝试在 catch 块之外运行它时,出现此错误:未处理的异常
    • @Jrawr 你还需要处理那个异常,比如this
    【解决方案2】:

    我认为 readLine() 不包括读取行末尾的换行符,因此您必须明确添加它。

        while ((line = bReader.readLine()) != null) {
            finalString.append(line);
            finalString.append('\n');
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 2019-02-05
      • 2020-11-21
      • 2015-07-25
      相关资源
      最近更新 更多