[java] How do I get a platform-dependent new line character?

How do I get a platform-dependent newline in Java? I can’t use "\n" everywhere.

This question is related to java cross-platform newline eol

The answer is


In addition to the line.separator property, if you are using java 1.5 or later and the String.format (or other formatting methods) you can use %n as in

Calendar c = ...;
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY%n", c); 
//Note `%n` at end of line                                  ^^

String s2 = String.format("Use %%n as a platform independent newline.%n"); 
//         %% becomes %        ^^
//                                        and `%n` becomes newline   ^^

See the Java 1.8 API for Formatter for more details.


This is also possible: String.format("%n").

Or String.format("%n").intern() to save some bytes.


The commons-lang library has a constant field available called SystemUtils.LINE_SEPARATOR


If you are writing to a file, using a BufferedWriter instance, use the newLine() method of that instance. It provides a platform-independent way to write the new line in a file.


Java 7 now has a System.lineSeparator() method.


You can use

System.getProperty("line.separator");

to get the line separator


Avoid appending strings using String + String etc, use StringBuilder instead.

String separator = System.getProperty( "line.separator" );
StringBuilder lines = new StringBuilder( line1 );
lines.append( separator );
lines.append( line2 );
lines.append( separator );
String result = lines.toString( );

StringBuilder newLine=new StringBuilder();
newLine.append("abc");
newline.append(System.getProperty("line.separator"));
newline.append("def");
String output=newline.toString();

The above snippet will have two strings separated by a new line irrespective of platforms.


If you are writing to a file, using a BufferedWriter instance, use the newLine() method of that instance. It provides a platform-independent way to write the new line in a file.


You can use

System.getProperty("line.separator");

to get the line separator


Java 7 now has a System.lineSeparator() method.


If you're trying to write a newline to a file, you could simply use BufferedWriter's newLine() method.


You can use

System.getProperty("line.separator");

to get the line separator


The commons-lang library has a constant field available called SystemUtils.LINE_SEPARATOR


This is also possible: String.format("%n").

Or String.format("%n").intern() to save some bytes.


Avoid appending strings using String + String etc, use StringBuilder instead.

String separator = System.getProperty( "line.separator" );
StringBuilder lines = new StringBuilder( line1 );
lines.append( separator );
lines.append( line2 );
lines.append( separator );
String result = lines.toString( );

StringBuilder newLine=new StringBuilder();
newLine.append("abc");
newline.append(System.getProperty("line.separator"));
newline.append("def");
String output=newline.toString();

The above snippet will have two strings separated by a new line irrespective of platforms.


If you're trying to write a newline to a file, you could simply use BufferedWriter's newLine() method.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to cross-platform

How to execute XPath one-liners from shell? iOS / Android cross platform development How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor? What is "stdafx.h" used for in Visual Studio? How to identify platform/compiler from preprocessor macros? How to get the home directory in Python? How to check if running in Cygwin, Mac or Linux? Automatically add all files in a folder to a target using CMake? How is Java platform-independent when it needs a JVM to run? Difference between "\n" and Environment.NewLine

Examples related to newline

How can I insert a line break into a <Text> component in React Native? Print "\n" or newline characters as part of the output on terminal Using tr to replace newline with space How to write one new line in Bitbucket markdown? Line break in SSRS expression How to insert a new line in Linux shell script? Replace CRLF using powershell How to write new line character to a file in Java What is the newline character in the C language: \r or \n? How to print values separated by spaces instead of new lines in Python 2.7

Examples related to eol

Replace CRLF using powershell Windows command to convert Unix line endings? EOL conversion in notepad ++ Force LF eol in git repo and working copy Choose newline character in Notepad++ How do I use System.getProperty("line.separator").toString()? Carriage Return\Line feed in Java How do I get a platform-dependent new line character? What's the best CRLF (carriage return, line feed) handling strategy with Git? When do I use the PHP constant "PHP_EOL"?