Fazal
Fazal

Reputation: 3051

Handling newline character in input between Windows and Linux

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.

  1. Does the incoming data contain "\r\n" or "\n"

  2. 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.

  3. 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?

Upvotes: 6

Views: 10774

Answers (5)

shashwatZing
shashwatZing

Reputation: 1630

Rather than using regular expression, you can also make it simpler by doing something like.

StringBuilder sb=new StringBuilder();
// append your texts here and to go to a new line use
    if(System.getProperty("os.name").startsWith("Windows")){

                sb.insert("\r\n");
            }
            else {
            sb.insert("\n");
    }

So if your local environment is windows , you can have this working locally and will also work if you're deploying to a different linux based environments.

Upvotes: 1

Moki
Moki

Reputation: 5371

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

Upvotes: 4

Fazal
Fazal

Reputation: 3051

Thanks for the response guys.. Finally looking at suggestion given by Kevin, we used StringReader and BufferedReader wrapper over it to overcome the issue. We used string reader as data is read as a string from the request.

Hopefully this question helps people in future

Upvotes: -1

dimitarvp
dimitarvp

Reputation: 2383

Probably try this?

String[] lines = inputString.split("\r?\n");

Not 100% sure about the syntax but the basic idea of the regex is: "zero or one \r, and exactly one \n". Or, if you just want to normalize the input:

inputString = inputString.replace("\r?\n", "\n");

Doesn't seem very painful to me. ;-)

Upvotes: 0

Kevin Brock
Kevin Brock

Reputation: 8944

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.

Upvotes: 3

Related Questions