0

I have problem with this annoying ^M, while exporting some data, writing it to a CSV file to be downloaded. I did some research and found out that if the file you are reading comes from a Windows system this issue happens (Windows uses CR (i.e. ^M)/LF pair to indicate the end of a line, while UNIX uses only a LF).

Now can anyone offer me a solution to overcome this problem (like eliminating or replacing ^M ) before putting it to the writer (writer.write(columnToBeInserted);)

3 Answers 3

2

You could use unix2dos and dos2unix to convert UNIX and Windows files respectively. Both are available on *nix and Windows platforms. Read more.

Links for Windows

Also see How to convert files from Dos to Unix in java

Sign up to request clarification or add additional context in comments.

5 Comments

Could you please provide an example so that I can use in my java code?
@ComeRun See the last link
Mate,Since this is a problem reported by one our customer, would u be able to instruct me on how to reproduce this char? like i need to send a text message using fronend and then create a report of that message (as CSV), I need to type something in the message textarea to create that issue and I am not sure how! any special code or something?
I'd say create a text file on Windows and FTP to Linux. Open with vi to view CTRL chars. Any code that uses, say a PrintWriter running on Windows should produce the same result.
To create on Linux, write a file containing "\r\n" line separators.
1

As you read each line do

line.replaceAll("\\p{Cntrl}", "");

Or use a tool to do it for you

1 Comment

Amir I m using a linux environment and I dont know how to reproduce this char to test your suggestion. would u be able to tell me if there is any special code to reproduce this ^M char?
0

in linux/unix environment there is a utilities called dos2unix and unix2dos which converts the files from windows to linux format and vise versa .

on windows check this link and download the utility whch will convert from windows to linux format http://www.sg-chem.net/u2win/

2 Comments

Can u please provide me with an example so that I can use in my java code? thanks
this is not done through java code. you pass the filename.java to as a command argument to this utility and it will do the conversion, you do this before commiting your code to csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.