PrintWriter to print on next line

2019-02-14 07:44发布

I have the following code to print a string(from a ResultSet) to a text file:

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. The txt file currently prints out like so, with row# being a different row in the ResultSet:

row1row2row3row4row5

I want it to print out like:

row1

row2

row3

row4

row5

...

3条回答
The star\"
2楼-- · 2019-02-14 08:16

You can use PrintWriter#println() method instead.

From 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').

Also this should work as well.

writer.write(RS.getString(1)+ System.getProperty("line.separator"));
查看更多
Deceive 欺骗
3楼-- · 2019-02-14 08:16

Use, in the statement. For example:

writer.print("1"+"<br/>");
writer.print("2"+"<br/>");
查看更多
放荡不羁爱自由
4楼-- · 2019-02-14 08:36

You should use println to print a newline character after each line:

writer.println(RS.getString(1));
查看更多
登录 后发表回答