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

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
ComeRun
  • 921
  • 1
  • 20
  • 38

3 Answers3

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

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Could you please provide an example so that I can use in my java code? – ComeRun Feb 04 '13 at 00:22
  • 1
    @ComeRun See the last link – Reimeus Feb 04 '13 at 00:26
  • 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? – ComeRun Feb 04 '13 at 00:48
  • 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. – Reimeus Feb 04 '13 at 00:51
  • 1
    To create on `Linux`, write a file containing `"\r\n"` line separators. – Reimeus Feb 04 '13 at 00:56
1

As you read each line do

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

Or use a tool to do it for you

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • 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? – ComeRun Feb 04 '13 at 00:40
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/

Saddam Abu Ghaida
  • 6,381
  • 2
  • 22
  • 29
  • Can u please provide me with an example so that I can use in my java code? thanks – ComeRun Feb 04 '13 at 00:21
  • 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 – Saddam Abu Ghaida Feb 04 '13 at 00:25