EN
Java - how to get new line character for given platform eg Windows, Linux or MacOS - platform dependent?
14
points
1. Overview:
In java it is possible to get platform dependent new line character in couple of ways:
System.lineSeparator()
System.getProperty("line.separator")
String.format("%n")
2. New line character on most popular operating systems
Operating System Name | New line character |
Windows | \r\n |
Linux / Unix | \n |
MacOS | \n |
3. Java code example
public class Example {
public static void main(String[] args) {
String newLine1 = System.lineSeparator();
String newLine2 = System.getProperty("line.separator");
String newLine3 = String.format("%n");
System.out.println(newLine1);
System.out.println(newLine2);
System.out.println(newLine3);
}
}
When running on Windows: