ComeRun
ComeRun

Reputation: 921

Java | CSV issue with ^M

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);)

Upvotes: 0

Views: 235

Answers (3)

Amir Afghani
Amir Afghani

Reputation: 38541

As you read each line do

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

Or use a tool to do it for you

Upvotes: 1

Reimeus
Reimeus

Reputation: 159844

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

Upvotes: 2

Saddam Abu Ghaida
Saddam Abu Ghaida

Reputation: 6749

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/

Upvotes: 0

Related Questions