1

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.

  • Is there a way to fix the issue without touching the java code?
  • Is there a way to write a java program to make it work on both kind of OSs (possibly all of them)?

Thank you

Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154

4 Answers4

4

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

Community
  • 1
  • 1
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
0

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

crowne
  • 8,456
  • 3
  • 35
  • 50
Pablo Grisafi
  • 5,039
  • 1
  • 19
  • 29
  • Rather than a custom function, see the line.separator system property mentioned by @Suraj – crowne Jun 23 '11 at 18:17
  • I rather use a custom method. I don't want to mess with system wide properties that can affect everything that runs in the same jvm. But it is up to you. – Pablo Grisafi Jun 23 '11 at 19:57
  • \r\n or custom functions will both link my jar to a specific OS, I was hoping in something more flexible. The line.separator would get the property of the system where the jar is launched, right? And this wouldn't work either if I save the file in a remote machine I guess.. Suraj's solution works well and require only a little extra configuration – Marsellus Wallace Jun 23 '11 at 20:07
0

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.

gouki
  • 4,382
  • 3
  • 20
  • 20
0

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.

joeslice
  • 3,454
  • 1
  • 19
  • 24