我正在 servlet 中使用 Printwriter 打印文本文件。

PrintWriter writer = response.getWriter();
while ((text = reader.readLine()) != null) 
        {
    writer.print(text);
        }

虽然这会打印 txt 文件,但它不会打印任何新行。

我试过了

LINE ="\n"
writer.print(text + LINE);

不起作用。

也尝试使用println,仍然没有用。

有什么建议么??

有帮助吗?

解决方案

使用<br>标记用于HTML。

LINE ="<br>"
writer.print(text + LINE);
.

其他提示

  1. 对于文本文件,我们需要将响应 contentType 设置为 text:例子: response.setContentType("text;charset=UTF-8");
  2. 对于换行者 PrintWriter 我们需要打印 println:例子: out.println("abcd....");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top