File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Security and the fly likes Remove ^M character in text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "Remove ^M character in text file" Watch "Remove ^M character in text file" New topic

Remove ^M character in text file

Sivaraman Lakshmanan
Ranch Hand

Joined: Aug 02, 2003
Posts: 231
Hi All,
I sftp (secure FTP) some text files from unix box to another unix box using the JSCH API. When the same file is FTPed I don't see the ^M character but the SFTped file has a ^M characters at the end of each line of the file. I cannot use any utility to remove the ^M characters. Is there any settings that I can do in the Unix box to solve this or is there any settings in the JSCH API to solve this.

Any help on this is very much appreciated.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
FTP has a setting to transfer files in binary mode instead of ASCII mode; that should prevent any changes from happening.
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

Perhaps Ulf has it backwards? If you send it binary, it is send as is. If yo used it as text, it allows line conversions.

Many SFTP implementations don't have modes like ftp did. And since OP talks about using an API, what is supported depends on the API.

If you can't use something nice, like dos2unix, you can always just chop off the ^M at the end of lines after you use a readline() to get the string.
I agree. Here's the link:
subject: Remove ^M character in text file
It's not a secret anymore!