0

我想打开一个 .txt 文件,用字符 ` 拆分内容,然后显示这个数组的内容,每个条目在一行中向用户显示。

我已经用 android 实现了类似的效果,所以我下面的代码基于此:

try {
        // open the file
        File myFile = new File(f + "mx.txt");
        FileInputStream fIn = new FileInputStream(myFile);
        BufferedReader myReader = new BufferedReader(new InputStreamReader(
                fIn));
        String aDataRow = "";
        String aBuffer = "";
        while ((aDataRow = myReader.readLine()) != null) {
            aBuffer += aDataRow + "\n";
        }

        // String loadeddata = aBuffer;
        String[] splitdata = aBuffer.split("`"); // recover the file and
                                                    // split it based on `

        myReader.close();

        System.out.println(Arrays.toString(splitdata));
        txtDataWillBe.setText(Arrays.toString(splitdata));

    } catch (Exception ez) {
        System.out.println("error in array building");
    }

数组加载正常,但在文本区域中显示为单行。

我的问题是,如何拆分数组并添加“\n”,或者是否有另一种方法来显示数组每行一个条目?

另外,如果需要,我可以防止文本区域扩展到打开的窗口之外并显示垂直滚动条吗?

谢谢你的帮助。安迪

4

1 回答 1

2

您可以循环使用String[] splitdata并组合每个String使用System.getProperty("line.separator");

String lines = "";
for(String line : splitdata){
lines = lines + line + System.getProperty("line.separator");
}
于 2013-03-11T11:47:23.267 回答