简体   繁体   English

PrintWriter在下一行打印

[英]PrintWriter to print on next line

I have the following code to print a string(from a ResultSet) to a text file: 我有以下代码将字符串(从ResultSet)打印到文本文件:

PrintWriter writer = new PrintWriter(new FileOutputStream(file, false));
while(RS.next()) {
    writer.write(RS.getString(1)+"\n");
}

I put a "\\n" in the write statement in hopes that it will print each row on a different line, but it failed. 我在write语句中加了一个“\\ n”,希望它会在不同的行上打印每一行,但它失败了。 The txt file currently prints out like so, with row# being a different row in the ResultSet: txt文件当前打印出来,因为row#是ResultSet中的另一行:

row1row2row3row4row5 row1row2row3row4row5

I want it to print out like: 我希望它打印出来像:

row1 ROW1

row2 2行

row3 ROW3

row4 ROW4

row5 ROW5

... ...

您应该使用println在每行之后打印换行符:

writer.println(RS.getString(1));

You can use PrintWriter#println() method instead. 您可以使用PrintWriter #println()方法。

From API: 来自API:

Terminates the current line by writing the line separator string. 通过写行分隔符字符串来终止当前行。 The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\\n'). 行分隔符字符串由系统属性line.separator定义,不一定是单个换行符('\\ n')。

Also this should work as well. 这也应该有效。

writer.write(RS.getString(1)+ System.getProperty("line.separator"));

Use, in the statement. 在声明中使用。 For example: 例如:

writer.print("1"+"<br/>");
writer.print("2"+"<br/>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM