views:

139

answers:

3

I have a linux server and many clients with many operating systems. The server takes an input file from clients. Linux has end of line char LF, while Mac has end of line char CR, and Windows has end of line char CR+LF

The server needs as end of line char LF. Using java, I want to ensure that the file will always use the linux eol char LF. How can I achieve it?

A: 

Could you try this?

content.replaceAll("\\r\\n?", "\n")
eumiro
i have already all new lines with "\n"
bilal
A: 

Use

System.getProperty("line.separator")

That will give you the (local) EOL character(s). You can then use an analysis of the incomifile to determine what 'flavour' it is and convert accordingly.

Alternatively, get your clients to standardise!

Visage
how will i use it? i don't need local settings, i always need settings for linux, i mean LF end of file, even if the file will be generated in windows
bilal
by the way i want to do this client side, not the server side.
bilal
+1  A: 

Combining the two answers (by Visage & eumiro):

EDIT: After reading the comment. line. System.getProperty("line.separator") has no use then.
Before sending the file to server, open it replace all the EOLs and writeback
Make sure to use DataStreams to do so, and write in binary

String fileString;
//..
//read from the file
//..
//for windows
fileString = fileString.replaceAll("\\r\\n", "\n");
fileString = fileString.replaceAll("\\r", "\n");
//..
//write to file in binary mode.. something like:
DataOutputStream os = new DataOutputStream(new FileOutputStream("fname.txt"));
os.write(fileString.getBytes());
//..
//send file
//..

the replaceAll method has two arguments, the first one is the string to replace and the second one is the replacement. But, the first one is treated as a regular expression, so, '\' is interpreted that way. So:

"\\r\\n" is converted to "\r\n" by Regex
"\r\n" is converted to CR+LR by java
lalli
nice but i cant do anything serverside. i need something to do client side. like converting maybe..
bilal
Do the same thing at client side. See the modified answer. What type of converting are you talking about? If you mean encoding then that's a separate problem, not related to EOLs.
lalli
Regardless of whether this answers the OP's question, why regex in first place? You aren't looking for patterns, but just for a fixed sequence of chars. Why then not just a chars-by-chars replace using `replace()` method?
BalusC
lalli, edited code works fine but i need output as string. how can i convert outputstream to string?
bilal
@bilal: Read this - http://stackoverflow.com/questions/216894/get-an-outputstream-into-a-string@BalusC: The code is not using regex, it's AVOIDING regex. char-by-char might be more efficient, but is more code! Imagine the code: if '\r' is found check if next char is '\n' but take care of the array bound too! if it is '\n', remove both the chars with a single '\n' (shifting all chars one by one then adjusting the string length OR splitting the string and rejoining first with '\n', then result with second), if it's not '\n', still replace the char, much simply this time.
lalli
@lalli i have already read this post but it uses bytearrayoutputstream. should i use bytearrayoutputstream instead of dataoutputstream in your code? and what do you mean with Obj.? i couldn't find writetostream method
bilal
Replace the first regex with `\\r\\n?` and you'll only have to call replaceAll once.
jason