1

I have an application where I want to write a string to the file as newline at EOF.

My program executes on Linux OS and the file that I want to to write resides in a windows shared folder.

Problem: I am able to add string to the file as a newline, when I run the program on windows OS using eclipse. But when deploy and run the app on Linux box, string gets added to the file but not as a newline.

Eg: Let’s say if I want to add a new string as ‘test user’ to the file, this is how it adds.

Hr usr 
Hi usr
Jk  usrtest user

I want it to be like

Hr usr
Hi usr
Jk usrt
test user

BufferedWriter bw = new BufferedWriter(new FileWriter(fileUrl, true)); 
bw.newLine();
//didn’t write newline when I deployed it to Linux box
bw.write();
//OR
bw.append(System.lineSeperator() + “mystring”);
//didn’t write newline when I deployed it to Linux box
//OR
bw.append(“\r\n” + “mystring”);
//didn’t write newline when I deployed it to Linux box

I am pretty sure code wise nothing is wrong, somewhere UNIX or Windows getting confused with the newline separation.

Any input would be greatly appreciated.

Thanks, Jeevan.

jeevan
  • 21
  • 5
  • 2
    In Windows, newline is `\r\n`, and on Unix it's just `\n`. – 001 Apr 30 '18 at 13:54
  • 1
    @JohnnyMopp : wanted to right the same comment as well (+1). Known and classic issue when working cross windows/linux. – Rann Lifshitz Apr 30 '18 at 13:55
  • @JohnnyMopp You're correct of course, but many native windows apps now also respect just `\n` as a line break. Notepad is now the only popular one I'm aware of that doesn't... – Michael Berry Apr 30 '18 at 13:57
  • As a side note - please consider upvoting my answer, or selecting it as the question's answer, if you found it helpful. – Rann Lifshitz May 01 '18 at 12:28

3 Answers3

1

The culprit is found here :

bw.append(“\r\n” + “mystring”);

As mentioned by Johnny Mopp, breakline behaves differently on Linux. For your Linux deployment you should use "\n" instead of "\r\n".

A classic solution to this problem is creating a Utility class/method which checks which OS the JVM is running on, then returning a value based on the OS type (a Map of such values would work nicely, IMHO).

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
  • Yes! you both are right, but I access the file on a windows machine using NotePad. – jeevan Apr 30 '18 at 14:00
  • @jeevan : Be careful with manipulating a file in windows which should later be handled under Linux. I had issues with editing a script file in windows, which then had all of its breaklines replaced with the windows style breaklines. This caused the script to become un-operational on linux...... As a side note - please consider upvoting this answer, or selecting it as the question's answer, if you found it helpful. – Rann Lifshitz Apr 30 '18 at 14:17
  • 1
    It doesn't let me up vote, because of my reputation level.! – jeevan May 01 '18 at 13:40
1

Other than \n as mentioned in other comments, you can try String.format("%n") for new line.

Or even you can use System.getProperty("line.separator");

Harshit
  • 5,147
  • 9
  • 46
  • 93
  • You can do [`System.lineSeparator()`](https://docs.oracle.com/javase/9/docs/api/java/lang/System.html#lineSeparator--) since Java 7. It's shorter and there are no ugly string literals. – Michael Apr 30 '18 at 14:02
  • @Michael, OP is already using the same. I was just providing the alternatives for which he may be comfortable with. – Harshit Apr 30 '18 at 14:04
1

In this case "\r\n" is working, I had a deployment issue, so it did not update the class in web logic sever while trying "\r\n".

Thanks for your interest in my post.

jeevan
  • 21
  • 5