Marsellus Wallace
Marsellus Wallace

Reputation: 18601

Java println: Windows vs Linux

I have a java program that writes a file to a remote machine file system using the jcifs library -samba stuff; SmbFile=>SmbFileOutputStream=>PrintStream and the I use the common println(String). Everything worked fine till I moved my application to a linux machine and now the printed file on my remote windows machine looks weird.

I believe the problem is how the two OSs handle the CR, LF that are inserted by the println() function. My 'jar' is executed once a day and it's triggered by the 'crontab' through a 'sh' launch file.

Thank you

Upvotes: 1

Views: 2626

Answers (4)

Pablo Grisafi
Pablo Grisafi

Reputation: 5047

If your java file runs in a linux machine println will use the linux standard "\n" If you want it to allways use \r\n, without changing system properties, simply replace your println("text") with print("text\r\n"). Or create a custom function printWindowsLine to do that

Upvotes: 0

joeslice
joeslice

Reputation: 3454

System.out.println is a PrintStream, which uses the environment setting for line.separator as its, well, line separator. One solution to your problem would be to pass in -Dline.separator argument.

Probably the best solution though is to just use view your file with a better editor. Notepad++ and many others on Windows will understand both types of line endings, while notepad will not. Even the builtin wordpad does a better job of displaying unix file endings.

Upvotes: 0

gouki
gouki

Reputation: 4402

You will have to change the new line separator in the file. In linux, there is a utility called dos2unix that converts the new line separator of an existing file from dos/windows to linux/unix.

Upvotes: 0

Suraj Chandran
Suraj Chandran

Reputation: 24791

Try playing around with the system property "line.separator". You can read this for reference.

Upvotes: 4

Related Questions