java中如何输出格式化的html

发布于 2024-09-02 03:36:01 字数 411 浏览 7 评论 0原文

我正在阅读这样的 html 文件:

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

我想将其显示在 GWT textArea 中,在其中我将其作为字符串提供。但该字符串失去了缩进,并以单行文本的形式出现。有没有办法以正确的格式显示它(带缩进)?

I'm reading an html file like this:

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 give it to as a String. But the string loses indentations and comes out as a one-liner text. Is there a way to display it properly formatted (with indentations) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光无声 2024-09-09 03:36:01

这可能是因为 readLine() 截断了行尾字符。为每一行再次添加它们。

除此之外,在循环中使用 StringBuilder 而不是使用 += 来处理 String

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

That's probably because readLine() chops off the end-of-line character(s). Add them yourself again for each line.

Besides that, use a StringBuilder instead of using += to a String in 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();
清君侧 2024-09-09 03:36:01

好吧,假设您的 textArea 能够理解 HTML(我具体不了解 GWT),为什么不在其前面加上

 前缀,然后附加 

您可能仍然需要转义所有 HTML 特殊字符,例如将 & 转义为 & 以及将 < 转义为 <。

Well, assuming your textArea understands HTML (I don't know GWT specifically), why don't you prefix it with <pre> then append </pre>?

You'll may still have to escape all the HTML special characters such as & to & and < to <.

咽泪装欢 2024-09-09 03:36:01

使用 FileReader 可能会更有效——没有理由必须逐行读取文本。正如 Jesper 所建议的,使用 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();

It might be more efficient to use a FileReader instead--there's no reason why you have to read the text line-by-line. Like Jesper suggested, using a StringBuilder to build your String is more efficient. Also, with FileReader, you don't have to manually append any newlines:

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();
风筝有风,海豚有海 2024-09-09 03:36:01

如果您的 HTML 恰好是 XHTML,那么您可以尝试的一件事是将其放入 XML 解析器(例如 jdom 或 dom4j)中,它们通常具有一些“漂亮打印”选项。

If your HTML happens to be XHTML, then one thing you can try is to put it into an XML parser such as jdom or dom4j, which usually has some "pretty-print" option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文