user694485
user694485

Reputation: 121

"\n" delimiters issue

I have a stringbuilder object, that a line of data gets added to.

after each line gets added, I append a "\n" on the end to indicate a new line.

this stringbuilder object, finalised, gets written to a flat file.

When I open the flat file in notepad I get a small rectangle after every line and the column formatting is ruined.

When I open the flat file in wordpad, the new line is taken into consideration and the column formatting is perfect.

I have tried all ways I know of removing the new line entry before it gets written, but this removes the formatting when written to the flat file. I need the new line for the formatting of the columns.

how can I output the file with new lines but without using \n?

Upvotes: 1

Views: 17652

Answers (5)

GustyWind
GustyWind

Reputation: 3036

You can get the value for the system your Java program is running on from the system properties

public static String newline = System.getProperty("line.separator");

Upvotes: 2

khachik
khachik

Reputation: 28703

You should add System.getProperty("line.separator") instead of \n. Since "nodepad", it is \r\n, for MS Windows.

Upvotes: 1

Jonathan
Jonathan

Reputation: 7604

If you're using Windows, you should be writing \r\n to get it to load properly in Notepad. The \n terminator is a Unix file ending, and Notepad won't parse it properly. Wordpad will convert them for you.

Also I suggest not using Notepad, and looking towards something like Vim.

Upvotes: 0

Koziołek
Koziołek

Reputation: 2874

In Windows you should use \n\r. In *NIX (Linux/UNIX/Mac) u should use \n

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503599

The Windows way of terminating a line is to use "\r\n", not just "\n".

You can find the "line separator for the current operating system" using the line.separator system property:

String lineSeparator = System.getProperty("line.separator");
...
StringBuilder builder = new StringBuilder();
...
builder.append(lineSeparator);
...

Upvotes: 11

Related Questions