【问题标题】:System.getProperty("line.separator") not working - Android StudioSystem.getProperty(“line.separator”)不起作用 - Android Studio
【发布时间】:2016-08-29 05:59:15
【问题描述】:

目前我有一个代码,用于检索网站纯文本并将其存储在文本文件中。我设法做到了,但是当我想在文本文件中输入新行时,它不起作用。

我的结果

This is the article titleThis is the starting of the first paragraph

我想要什么

This is the article title
This the starting of the first paragraph

我的源代码

public void storeHTML(Context context, ArrayList<String> storeHTML, String address) {
    try {
        File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/voicethenews");
        if (!root.exists()) {
            root.mkdirs();
        }

        address = address.substring(address.lastIndexOf("/") + 1);

        File gpxfile = new File(root, address + ".txt");
        FileWriter writer = new FileWriter(gpxfile);

        //FileOutputStream fileOutputStream = new FileOutputStream(gpxfile);
        //BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));

        for(int i = 0; i < storeHTML.size(); i++) {

            //bufferedWriter.write(storeHTML.get(i));
            //bufferedWriter.newLine();
            writer.append(storeHTML.get(i) + System.lineSeparator());


        }

        writer.flush();
        writer.close();
        Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我尝试了多种代码和解决方案,但仍然无法正常工作。

【问题讨论】:

标签: java android


【解决方案1】:

我找到了它未按预期显示的实际原因。

首先它似乎不起作用的原因是我使用默认记事本打开文本文件。当使用其他文本编辑器(如 notepad++)打开时,文本文件中的输出按预期写入。

【讨论】:

    【解决方案2】:

    如果您从 HTML 中获取详细信息。所以,让我们使用&lt;br&gt;,而不是&lt;br \&gt;

    这是一个将文本添加到 textView b的示例代码

    b.setText(Html.fromHtml("<b>" + st + "</b>" + "<br/>" + cursor.getString(1)));
    

    参考:How do i add a new line in html format in android?

    【讨论】:

      【解决方案3】:

      这个问题可能正在解决您遇到的完全相同的问题:

      System.lineSeparator() doesn't exist in android

      在 1.7 之前的 SDK 中存在一个错误,您必须直接调用环境变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-05
        • 2011-03-31
        • 1970-01-01
        • 2016-08-16
        • 1970-01-01
        • 1970-01-01
        • 2020-05-16
        相关资源
        最近更新 更多