I think this is a standard problem which may have been asked before but I could not get the exact answer so posting the issue.
The issue is that our server is running on a linux box. We access the server over the browser on a window box to enter data into field which is supposed to contain multiple lines which user can enter by pressing the enter key after each line Abc Def GHI
When this input field (this is a text area),is read on the linux machine, we want to split the data based on new line character.
I had three question on this.
Does the incoming data contain "\r\n" or "\n"
If incoming data does contain "\r\n", the linux line.separator property (vm property) would not work for me as it would say "\n" and therefore may leave "\r" in the data.
If "\r" is left in the data, if I open the file on a windows machine, will this mean a newline character?
Finally can anyone tell me the standard way to deal with this issue?
The standard java.io.DataInputStream
and java.io.BufferedInputReader
both handle this automatically through the readLine() method. You really should not use DataInputStream
for this since it does not support character sets correctly and it's readLine() has been deprecated for a long time.
For text output, you can use java.io.PrintWriter
in which it's printLn(), and related methods with parameters, output the correct newline sequence for the current platform. java.io.BufferedWriter
also handles this correctly and provides a public
newLine() method.
Linux uses \n
.
Windows uses \r\n
.
Therefore, unless you've tweaked something in linux, it should be coming in \n
.
You could regex out \r\n
and \n
and replace with whatever you want to avoid problem 3.
http://en.wikipedia.org/wiki/Newline
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With