Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File I/O problem: Java program inserts ^M into text file  RSS feed

 
Michael K. Wilson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a Java program which looks like this:



When I open up this file using a text editor, I see a bunch of ^M's everywhere!

How do I get rid of these symbols (^M)?

I am using Eclipse 3.4 on Windows Vista...

-Mike
[ July 18, 2008: Message edited by: Michael K. Wilson ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
That symbol usually comes up when you transfer a file from UNIX to Windows. Did you do anything on the UNIX operating system? What program did you use to create the file.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael K. Wilson:
How do I get rid of these symbols (^M)?

To actually get rid of them, open the file in WordPad. This will show it without the ^M characters. Then copy it and paste it into notepad and save. This will make the characters gone from the actual file.

My previous post was more to help you figure out how the ^M got there so you don't get them next time.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The println method adds a line ending that's appropriate for the platform. On Windows that's the sequence "\r\n" (CR followed by LF). The Java GUI components, on the other hand, use just the LF character to indicate a newline, so the CR is displayed as an extraneous character.

If you don't want to create the CR in the first place, use p.print(TABBED+"Hello"+"\n") instead (note the missing "ln").
 
Michael K. Wilson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf and Jeanne,

Thanks for the feedback!

Jeanne-

I use Windows Vista for everything and that's why your response threw me off...

Ulf-

What you are saying is that don't use printlns and only use print("\n") for everything?

This makes a lot more sense and I will try it, first thing in the morning!

Greetings,

Mike
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are saying is that don't use printlns and only use print("\n") for everything?

No, that's not quite what I'm saying. What I'm describing is a workaround for this particular situation.

Most programs are smart enough to know about the line ending issue by now, and won't display extraneous CR characters. So it doesn't make sense to forgo the println method in general and deal with adding the LF yourself all the time.
But there are some Windows programs that will look specifically for CR+LF to indicate a line ending, and if they see only LF, they will assume that that's not a line ending - and consequently treat the entire file as a single line of text. But that's much rarer now than it used to be.

A bit later: I just reread your initial post more carefully, and noticed that you're not looking at the file in Eclipse, but in a text editor (so my remark about GUI components was way off). In that case, check the editor's settings/options - there should be something in it about how to treat line endings. There may even be a menu that tells the editor what kind of file you're looking at -Windows or Unix- which should make this problem disappear.
[ July 18, 2008: Message edited by: Ulf Dittmer ]
 
Michael K. Wilson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

The program was written in Eclipse and the data files were opened in Eclipse.

I could swear that I opened it in a different editor such as text pad. Okay, will look into this and let you know the outcome! This does sound very promising... But I do think that I still opened them up in Eclipse.

Talk to you soon.

-Mike
 
Anton Spinner
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution to your problem requires a slight adjustment.

instead of using:


add the following to the top of your class:


when you need a new line then use LINE_SEPARATOR String. That String will have the correct information regardless of the platform on which your application used.

NB: java 5 and 6 added append() to writers so you can use that if you are inconvenienced by using character and byte arrays instead of strings.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove ^M (caret-M) from a specific file in Eclipse by choosing File --> Convert Line Delimiters To --> Windows.

Set up eclipse so this doesn't happen:

Window --> Preferences --> General --> workspace --> Make sure Default is checked or choose windows..
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
Michael,
That symbol usually comes up when you transfer a file from UNIX to Windows. Did you do anything on the UNIX operating system? What program did you use to create the file.

I thought it was the other way around? I've only seen them when I was using vi or nano on DOS / Windows text files. When I try to open a UNIX text file in notepad the line breaks usually show up as [] because of the missing carriage return.

Perhaps Michael's text editor is using the UNIX file format, even though it is on Windows.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!