How to output formatted html in java

I read the html file as follows:

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();
}

And I want to display it in a GWT textArea in which I pass it as a String. But the line loses deepenings and is displayed as single-line text. Is there a way to display it properly formatted (with recesses)?

+3
source share
4 answers

Probably because it readLine()breaks the end of line characters. Add them again for each row.

Also, use StringBuilderinstead of using +=in Stringin a 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();
+5
source

, textArea HTML ( GWT), <pre>, </pre>?

HTML, & &amp; < &lt;.

+1

, FileReader - , . , StringBuilder String . , FileReader :

StringBuilder sb = new StringBuilder();
FileReader in = null;
try {
    in = new FileReader(path);
    int read;
    char buf[] = new char[4096];
    while ((read = in.read(buf)) != -1) {
        sb.append(buf, 0, read);
    }
} catch (Exception e) {
    return e.getMessage();
} finally {
    in.close();
}

String result = sb.toString();
+1

HTML- XHTML, XML, jdom dom4j, "pretty-print".

0

Source: https://habr.com/ru/post/1745967/


All Articles