This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File upload and ^M chars  RSS feed

 
Ramu Nur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I wrote a code using apache' fileUpload and when a file is uploaded from Windows to Unix env, there are ^M chars at the end. This code is deployed on Unix, but a file can be uploaded from Windows. I know how to use some unix commands or dos2unix from windows to remove the ^M chars, but this has to be done programmatically.

A byte array of windows data is written to file output stream to unix env.

Any clue...

thanks
Rams
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^M is the return character - a hallmark of text files created on Windows. I bet the characters are there before the file gets uploaded, it's just that the Window applications don't display it. Do a byte-for-byte comparison of the file before and after the upload using a hex editor or a diff tool - they're in all likelihood identical.
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Windows uses \r\n for line breaks - carriage return followed by line feed. Unix only uses \n. The ^M you're seeing is the \r character that isn't recognized by your Unix text editor (don't tell me: VI or Emacs; I've seen them in VI as well).

To remove these you can read the file using a BufferedReader and its readLine() method, then write it to a new file. The readLine() method strips away all line breaks, including \n and \r\n. The writing will use the system default for line breaks, so \n on Unix.

But keep in mind that the new file will not be identical to the original file. If the file is later retrieved and downloaded by a Windows user then the lack of \r may cause problems in Windows text editors. Especially Notepad is notorious for displaying little blocks (like []) when encountering \n without a preceding \r.
 
Ramu Nur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
Thanks. I removed all '\r' before uploading, and working fine.

--Rams
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!