jQuery in Action, 3rd edition
The moose likes IDEs, Version Control and other tools and the fly likes I want to import single-spaced code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "I want to import single-spaced code" Watch "I want to import single-spaced code" New topic

I want to import single-spaced code

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

When I receive source code from other programmers of from some examples that I download (such as the examples from the "Core Java" books, the code is double-spaced when I open the file in Eclipse.

I imagine that the problem might be that DOS/Windows uses a carriage-return and a line-feed to terminate a line and Unix (including Mac OS X which I''m using), uses a single line-feed to terminate each line.

Will someone do me the favor of telling me how to open .java files that I get from other people and have them single-spaced so that I don't have to go through the entire file and take out all of the extra blank lines?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11778

Originally posted by Kaydell Leavitt:
I imagine that the problem might be that DOS/Windows uses a carriage-return and a line-feed to terminate a line and Unix (including Mac OS X which I''m using), uses a single line-feed to terminate each line.

I don't think that's the problem, but unfortunately I don't know what the solution is.

I created a simple "Hello, world" program on my Mac, and forced it to be in DOS text format.

When I edit the source file using "vim Hello.java", I get:
So I can see on the status line that it recognized this as a DOS file and did an auto-convert for me.

If I forcibly tell vim not to auto-convert for me by typing "vim -b Hello.java", I get:
Now I can see a representation of all the carriage returns (the ^M which corresponds to ctrl-M) at the end of each line.

More importantly, when I load this DOS file into Eclipse on my Mac, it handles the carriage returns perfectly - I see the file single spaced.

Anyway - if the problem was the carriage returns, then you could remove them while in the vi editor by typing ":%s/^M//g" (minus the quotes of course). To type the control-M, you would actually have to type control-V (to specify that the next character should be used verbose - no translation) and then type control-M).

Alternatively, you could do the same thing from the command line. Something like "sed 's/^M//g' Hello.java > Hello2.java" would do the trick (again, not typing the double quotes). You could easily script this to work with all the files you have to convert rather than do them manually.

Alternatively, if you have Fink installed, you can use it to easily install the "dosunix" package, which describes itself as:
Converts DOS text files to unix text format


DosUnix is made up of three programs: - dosunix, which creates a copy of a DOS text file
in Unix text format.
- unixdos, which creates a copy of a Unix text file
in DOS text format.
- chktxt, which inspects a given text file to determine
whether it is in DOS text format or Unix text format.

Web site: http://dosunix.sourceforge.net

Maintainer: None <fink-devel@lists.sourceforge.net>

Unfortunately, while I have given a number of solutions to your question of how to convert the files, I doubt that I have solved your problem, as Eclipse should handle the files without problem.

One thing you could try is taking a look at one of the files in byte format. For example:
od provides octal dumps, which personally I find very trying . Fortunately it does far more than that - for example, the -a flag tells it to still spit everything out byte by byte, but give ascii equivalents for everything. So it is easy for me to visually verify that this file does have both carriage returns and newlines.

Perhaps you could run this command on one of your files and determine what it is that is causing the problem?

Some examples I can think of:
  • The file is actually double spaced (why, I don't know)
  • The file has mixed formatting - some lines only have line feeds, some have both carriage return and line feed. Most editors wont know what the safe thing to do is in that situation, so they default to assuming you want everything verbose
  • The file has newlines followed by carriage returns (I think this was old OSX format, or was it VMS - it is too long since I had to deal with that format).

  • Regards, Andrew
    [ March 30, 2008: Message edited by: Andrew Monkhouse ]

    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    Kaydell Leavitt
    Ranch Hand

    Joined: Nov 18, 2006
    Posts: 689

    Thanks. That give me a lot of things to try

    -- Kaydell
    Ilja Preuss

    Joined: Jul 11, 2001
    Posts: 14112
    As a workaround, you can configure the Eclipse code formatter to remove superfluous empty lines.

    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
    Tim Holloway
    Saloon Keeper

    Joined: Jun 25, 2001
    Posts: 17423

    Eclipse is pretty intelligent about the DOS/Unix/Mac end-of-line issues. Generally, it'll understand them all and keep the files as it received them. There's a menu option to alter that on a per-file basis.

    It's possible that the offending files were simply malformatted when they were prepared for publication. If you could do a binary dump of them, it might show what's wrong.

    If you do routinely have some sort of issue, you might want to use one of the Linux text utilities to clean them up (if you're running cygwin under Windows, the same utilities can be used). Or, just write a simple Java application to toss out the extra lines.

    An IDE is no substitute for an Intelligent Developer.
    Kaydell Leavitt
    Ranch Hand

    Joined: Nov 18, 2006
    Posts: 689

    Thanks for all of the help.

    I think that the problem is that I have the .java extension associated with the Mac's XCode IDE so when I open Java source files, they open in XCode. Then I was copying and pasting into Eclipse. This is where the problem happens.

    Now, If I open the original Java source-files in BBEdit and then copy and paste into Eclipse, there is no double-spacing. This works, but it seems like I should learn how to import .java files directly into Eclipse. I think that this would be using the "Import" menu-item in Eclipse.
    I agree. Here's the link: http://aspose.com/file-tools
    subject: I want to import single-spaced code
    It's not a secret anymore!