Rahul Kumar
Rahul Kumar

Reputation: 399

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

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 
.

Upvotes: 4

Views: 13273

Answers (3)

Ester Rose
Ester Rose

Reputation: 61

use out.write(10); to add new line. 10 is acsii character for newline. But it is not work for Indirect/Direct Buffer type FileChannel.

Upvotes: 0

user207421
user207421

Reputation: 311023

  • 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".

Upvotes: 6

zeyorama
zeyorama

Reputation: 445

Very simple

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

That's all.

Upvotes: 4

Related Questions