PrintWriter to print on the next line

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

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

I put "\ n" in the write statement, hoping that it would print each line on a different line, but that failed. Currently, the txt file is printed like this: line # is another line in the ResultSet:

row1row2row3row4row5

I want it printed as:

row1

row2

row3

row4

row5

...

+4
source share
3 answers

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

 writer.println(RS.getString(1)); 
+13
source

Instead, you can use PrintWriter # println () .

From the API:

Ends the current line by writing a line separator line. The line separator string is determined by the property of the line.separator system and is not necessarily a single newline character ('\ P').

It should also work.

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

Use in the instructions. For instance:

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

Source: https://habr.com/ru/post/1469049/


All Articles