System.lineSeparator() vs line.separator property
Java 7 introduced System.lineSeparator() as a more convenient and readable way to get the system's platform-dependent line separator string.
How it Works
Internally, it still queries the 'line.separator' property but wraps it in a method call. This avoids the need for System.getProperty("line.separator") and makes the code cleaner.
Java Example
public class LineSeparatorExample {
public static void main(String[] args) {
String separator = System.lineSeparator();
System.out.print("Line 1" + separator + "Line 2");
}
}
Output
Line 1 Line 2
Key Points
- Platform-independent newlines
- Cleaner alternative to getProperty()
- Standardized in Java 7
- Ensures correct file formatting across OSs
Comments