Node.JS
Node.JS

Reputation: 1570

Java formatter going to new line

I am wondering is there any way after it writes to the txt file it goes to the next line ?

stdIn2.format("%s", x[i]);

like

a 
b   
c

I have tried stdIn2.format("%s\n", x[i]); but it does't work

Upvotes: 0

Views: 2176

Answers (5)

Burkhard
Burkhard

Reputation: 14738

Using notepad.exe is not the best choice.

In Windows a new line is defined by two chars: '\n' and '\r' In Linux it is only one '\n' (In MacOs it is only '\r' iirc)

Your programm only outputs a '\n' which won't be recognised as a new line by notepad. Notepad++, which is way better than notepad, will show the result as expected.

Upvotes: 0

Stegrex
Stegrex

Reputation: 4024

How about:

stdIn2.format("%s%n", x[i]);

The %n stands for platform-specific line separator in the formatter:

http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

Upvotes: 3

Yogendra Singh
Yogendra Singh

Reputation: 34367

Try using lineSeparator() as :

     stdIn2.format("%s"+System.lineSeparator(), x[i]);

This will append \r\n as line separator in Windows based environment.

Upvotes: 0

Makoto
Makoto

Reputation: 106410

You can use a PrintWriter, and follow the conventions listed in the API. println() will be familiar to you immediately.

PrintWriter out = new PrintWriter(new File("out.txt"));
out.println(String.format("%s", x[i]));

Upvotes: 2

AlexR
AlexR

Reputation: 115328

Use System.out.println(String.formtat(...)). This will definitely work.

Upvotes: 1

Related Questions