Writing new line character in Java that works acro

2019-05-11 19:59发布

问题:

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.

回答1:

No. You need to pick a single file format, Windows or UNIX. Given that your customer requires notepad, you can probably pick Windows format, which means you just need "\r\n" characters. The fact that UNIX generates the file can be considered irrelevant.



回答2:

Since Java 7 System.lineSeparator() ..

I bumped into this question and there are chances that others might do the same. I understand it is a pretty old post but as we all know, Java APIs keep changing with time. IMO 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. Please check this, System.lineSeperator()



回答3:

Mac OS X supports CR, LF and CRLF Linux uses LF, but files using CRLF and CR render too. Windows uses CRLF

the answer: just always use CRLF, then you have support for all platforms.

CR = '\r' , LF='\n'

so just make strings of the format : "mytexthereblahblahblah \r\n"



回答4:

System.getProperty("line.separator");

This is wrong. You don't care what the system's line separator is. You need to write out a file in the format that file is supposed to be in. It doesn't matter what system generates the file.

A file is a stream of bytes. You need to write out the stream of bytes that the file format says you should be writing, and that is independent of what line separate any system uses.



回答5:

Have you checked if the UNIX server can tolerate Windows style newlines? Since the requirement is editting in Notepad, you will need to put Windows style newlines in the file using System.print("\r\n").

If the UNIX server can't handle Windows line separators, then your best option is probably to process the file after editting to convert the newlines using dos2unix on the UNIX server.

The Notepad requirement means that this isn't really a platform independence requirement - it's a requirement for the Unix server to understand Windows newlines.



回答6:

There is no direct way for having such interoperable simple text file. Hence will have to move to a structured file like doc or excel or pdf or simply fix the format to either windows or UNIX and communicate the end users to use the same.

This answer is an aggregate of the above discussion.

Thanks a lot community for helping. If any other solution is found to the problem. Please post for others :)