Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between System.getProperty("line.separator"); and "\n"?

Tags:

While developing GUI with Java FX, I seem to get different results with System.getProperty("line.separator"); and "\n" during writing to a file or getting data from internet. What basically is the difference?

like image 948
Tilak Maddy Avatar asked Apr 22 '16 14:04

Tilak Maddy


People also ask

What is a line separator?

The line separator used by the in-memory representation of file contents is always the newline character. When a file is being loaded, the line separator used in the file on disk is stored in a per-buffer property, and all line-endings are converted to newline characters for the in-memory representation.

Is the line separator the same on all platforms What is the line separator on Windows?

The line separator varies from computer to computer, from Operating System to Operating System. You will get different line separators on Windows and Unix. If you run this: System. getProperty("line.

How do you use a line separator?

Strings have no newlines. We can form them into two lines by concatenating a newline string. Use System lineSeparator to get a platform-dependent newline string.

How does system getProperty work in Java?

The getProperty(String key) method in Java is used to returns the system property denoted by the specified key passed as its argument.It is a method of the java. lang. System Class. where key is the name of the System property.

What is system line separator in Java?

System.lineSeparator () method in Java With Examples. The lineSeparator () is a built-in method in Java which returns the system-dependent line separator string. It always returns the same value – the initial value of the system property line.separator.

What is the difference between '' \N'' and ''LINE SEPARATOR''?

System.getProperty("line.separator") is different from "\n" in that the former returns the OS line separator (not always \n). \n is just a line feed, and when you open your output file in a program that does not interpret \n as a new line (say, Notepad on Windows) you won't see that new line.

What is the difference between getProperty () and getproperties () methods?

The simpler of the two getProperty methods takes a single argument. getProperties: The java.lang.System.getProperties () method determines the current system properties. getProperty (String key) : java.lang.System.getProperty (String key) method returns a string containing the value of the property.

How do I get the line separator of a given line?

If you run this: System.getProperty ("line.separator") it will return the line separator that is specific to your OS. On windows it will return and on Unix it will return .


1 Answers

System.getProperty("line.separator") returns the OS dependent line separator.

On Windows it returns "\r\n", on Unix "\n". So if you want to generate a file with line endings for the current operating systems use System.getProperty("line.separator") or write using a PrintWriter.

like image 176
wero Avatar answered Sep 28 '22 17:09

wero