Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

out.write() in java , how to insert newline

Tags:

java

When writing to a text file in java , how do I enter values into a new line

code snippet

while (rs.next()) {
                int sport = rs.getInt("sport");


                String name = rs.getString("name");


                out.write(sport + " : " + name);}

the text file populates " value1 value2 value3...etc" I want it to populate

value1
value2
value3 
.
like image 952
Rahul Kumar Avatar asked Dec 26 '22 10:12

Rahul Kumar


2 Answers

  • If 'out' is a PrintWriter, use println().
  • If 'out' is a BufferedWriter, use newLine().
  • If 'out' is some other Writer, use write('\n'), or append the newLine directly to the string you're writing. If you want the system's line separator, see System.getProperty() with the value "line.separator".
like image 148
user207421 Avatar answered Jan 18 '23 16:01

user207421


Very simple

out.write(sport + " : " + name + "\n");

That's all.

like image 20
zeyorama Avatar answered Jan 18 '23 17:01

zeyorama