Banging my head with Java line separator

One of the most unpleasant experiences with computers is having to solve a problem you already fixed when you
realize the old solution is no longer working for reasons unknown.

The problem this morning was quite silly, actually. One piece of code created a text file. I did not care much about the way newlines were represented but when  I sent the file to to a colleague that uses Windows he complain notepad was making a mess out of it.

It was clear notepad was doing this because my file, created on a unix system, did not honor Windows end of line convention of CR plus LF characters (0x0d + 0x0a). I have solved that in the past just changing the System property called line.separator. You could do that with System.setProperty("line.separator","\r\n") but apparently not anymore. Of course, no error message or exception is raised, so you are left alone, in the dark, trying to figure out what is wrong and why what worked before does not work now.

I could not make much sense out of it, but I hate these changes that make me waste my time for no known good reason. It just seems the JVM ignores my changes to the property, still named the same way, for the same purpose. It vaguely mentions that a SecurityManager might be needed but I did not explore the problem further, I just learned that, without changing the code, I could use java -Dline.separator=$'\r\n' MyProgram to get it working with the new value for line separator property, so I did and got the problem solved, or almost all.

A few lines of my text file came from a configuration file that, again, did not follow Windows convention. So as I wanted to get the problem sorted out for all the lines and not only those generated from Java code, I had to figure out how to, easily, use vi to replace the LF end of lines by the real deal CR LR. I guess I did that once or twice years ago but I could not remember how to do it now. Long story short: %s/\n/\^M\r/g and please note that for obtaining ^M you need to press Ctrl+V and then Ctrl+M. I am thankful the backslash preceding ^M was mentioned in stackoverflow as its need was unclear to me.

A simpler way of getting a Windows-friendly text file from vi is to use the command :set fileformat=dos


Popular posts from this blog

VFD control with Arduino using RS485 link

4xiDraw: Another pen plotter

Arduino mood light