Is there a Newline constant defined in Java like Environment.Newline in C#?

In C# there is the static property Environment.Newline that changed depending on the running platform.

Is there anything similar in Java?

Asked by: John958 | Posted: 28-01-2022

Answer 1

As of Java 7 (and Android API level 19):


Documentation: Java Platform SE 7

For older versions of Java, use:


See for other properties.

Answered by: Daisy747 | Posted: 01-03-2022

Answer 2

As of Java 7:


Java API : System.lineSeparator

Returns the system-dependent line separator string. It always returns the same value - the initial value of the system property line.separator. On UNIX systems, it returns "\n"; on Microsoft Windows systems it returns "\r\n".

Answered by: Miranda800 | Posted: 01-03-2022

Answer 3

Be aware that this property isn't as useful as many people think it is. Just because your app is running on a Windows machine, for example, doesn't mean the file it's reading will be using Windows-style line separators. Many web pages contain a mixture of \n and \r\n, having been cobbled together from disparate sources. When you're reading text as a series of logical lines, you should always look for all three of the major line-separator styles: Windows \r\n, Unix/Linux/OSX \n and pre-OSX Mac \r.

When you're writing text, you should be more concerned with how the file will be used than what platform you're running on. For example, if you expect people to read the file in Windows Notepad, you should use \r\n because it only recognizes the one kind of separator.

Answered by: Marcus150 | Posted: 01-03-2022

Similar questions

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)