How to output formatted HTML in Java

I'm reading this HTML file:

try {
    BufferedReader bufferReader = new BufferedReader(new FileReader(path));
    String content;
    while((content = bufferReader.readLine()) != null) {
        result += content;
    }
    bufferReader.close();

} catch (Exception e) {
    return e.getMessage();
}

I want to display it in GWT textarea, and I give it as a string However, the string will lose indentation and appear as single line text Is there any way to display the format correctly (with indentation)?

Solution

This may be because readline() deletes the end of line characters Add them again for each row

In addition, use StringBuilder instead of string in = loop:

try {
    BufferedReader bufferReader = new BufferedReader(new FileReader(path));
    StringBuilder sb = new StringBuilder();
    String content;
    while ((content = bufferReader.readLine()) != null) {
        sb.append(content);
        sb.append('\n');   // Add line separator
    }
    bufferReader.close();
} catch (Exception e) {
    return e.getMessage();
}

String result = sb.toString();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>