1

How to read lines in a file using Java without losing the tabs, spaces in the beginning (indent)? I need this to read a sourcecode and than to print it out.

I am doing it like this:

        br = new BufferedReader(new FileReader(filePath));
        String line = null;
        while ((line = br.readLine()) != null) {
            aList.add(line);
        }

(of course with try catch blocks)

Thank you!

4

1 に答える 1

2

aListおそらくJListのように見えますが、レンダリング中にタブ文字を削除しています。

1 つの解決策は、タブをスペースに置き換えることです。

aList.add(line.replaceAll("\t", "    "));

さらに別の解決策は、JTextPane を使用して独自の ListCellRenderer を作成することですが、これには落とし穴がないわけではありません。

于 2012-08-16T20:23:11.613 に答える