PrintWriter to print on next line

北城余情 提交于 2019-11-30 05:25:17

问题


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

...


回答1:


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

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



回答2:


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



回答3:


Use, in the statement. For example:

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


来源:https://stackoverflow.com/questions/15393699/printwriter-to-print-on-next-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!