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?
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.
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.
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.
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.
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.
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.
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.
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 .
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With