Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing new line character in Java that works across different OS

I have a Java program which generates a text file on a UNIX server. This text file is then sent to a customer's machine using other systems where it is supposed to be opened in notepad on Windows. As we are well aware that notepad will not be able to find a new line as it uses Windows CR LF.

I am using

System.getProperty("line.separator");

Is there another way of acheiving platform independece so that a file generated on UNIX can be displayed in Windows notepad with the proper formatting like changing some UNIX property for new line?

Notepad is a pre-requisite, hence changing that is not possible.

like image 240
Anish Mohile Avatar asked Jan 17 '12 10:01

Anish Mohile


People also ask

How do you make a new line character in Java?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

What is line separator in Java?

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. Syntax: public static String lineSeparator()


1 Answers

Since Java 7 System.lineSeparator() ..

Java APIs keep changing with time. Writing new line character in Java that works across different OS could be achieved with System.lineSeparator() API which has been there since Java 7. check this, System.lineSeperator()

like image 177
Sudhansu Choudhary Avatar answered Oct 02 '22 01:10

Sudhansu Choudhary