views:

499

answers:

4

I have developed a number of classes which manipulate files in Java. I am working on a Linux box, and have been blissfully typing new File("path/to/some/file");. When it came time to commit I realised some of the other developers on the project are using Windows. I would now like to call a method which can take in a String of the form "/path/to/some/file" and, depending on the OS, return a correctly separated path.

For example:
"path/to/some/file" becomes "path\\to\\some\\file" on Windows.
On Linux it just returns the given String.

I realise it wouldn't take long to knock up a regular expression that could do this, but I'm not looking to reinvent the wheel, and would prefer a properly tested solution. It would be nice if it was built in to the JDK, but if it's part of some small F/OSS library that's fine too.

So is there a Java utility which will convert a String path to use the correct File separator char?

+3  A: 

Apache Commons comes to the rescue (again). The Commons IO method FileNameUtils.separatorsToSystem() will do what you want.

Needless to say, Apache Commons IO will do a lot more besides and is worth looking at.

Brian Agnew
Why do people keep suggesting adding bloated dependencies to avoid writing one line of code?
cletus
I don't regard Apache Commons as a bloated dependency. It does so much that I tend to regard Apache Commons Lang and IO as near-essential nowadays.
Brian Agnew
Plus, how many bugs do you find in single lines of code ? How many assumptions and edge-cases get missed ?
Brian Agnew
Because an external dependency has never had or introduced a bug?
cletus
To be honest it's almost inevitable that a project your are working on will have a dependent jar and that jar is going to have a Commons Collections/Lang/BeanUtils dependency (I'm surprised at how often this is true) -- so you would already have 1 or more Commons jars for runtime dependency so you could just use it for your compile time
non sequitor
Using commons has a couple of advantages in my particular scenario: it comes with a decent level of a guarantee that it will work and is tested; it's self documenting, unlike a regex which I would extract into a static helper method anyway; it's a university project, so not all of us are well versed in regular expressions. Looking at the source, the solution in commons is pretty similar to what you posted, cletus, so it doesn't make much difference. I did specify in the question that using a small library is fine, so +1.
Grundlefleck
@Cletus: it seems possible that he may already depend on commons-io if he's doing work with Files.
Paul Morie
I don't see why in anything other than the most constrained scenarios using a Commons IO/Lang library is unusual or unwarranted.
Brian Agnew
+5  A: 

A "/path/to/some/file" actually works under Windows Vista and XP.

new java.io.File("/path/to/some/file").getAbsoluteFile()

> C:\path\to\some\file

But it is still not portable as Windows has multiple roots. So the root directory has to be selected in some way. There should be no problem with relative paths.

Edit:

Apache commons io does not help with envs other than unix & windows. Apache io source code:

public static String separatorsToSystem(String path) { 
    if (path == null) {
     return null;
    }
    if (isSystemWindows()) {
      return separatorsToWindows(path);
    } else {
      return separatorsToUnix(path);
    }
}
Thomas Jung
+1: Exactly. I was about to post this answer until I saw one already did.
BalusC
Oh, I should add: the root disk which is going to be used is the same root disk as where the current working directory is (from where you have executed the Java code in question).
BalusC
Will it work on every platform which supports Java though?
Grundlefleck
See edit in my answer. Same as the commons io code. Will work with Windows and Unix (Linux, MacOs)
Thomas Jung
All windows platforms supports it. All UNIX platforms (and clones like Linux, Mac, AIX, Solaris and so on) supports it. I can only not tell from experience if it works in for example JSOS, Amiga, C64 and so on, but you don't need to worry as there's no JVM out for them (yet?).
BalusC
VMS or OS/390? There are some Java implementations for these.
Thomas Jung
@Thomas: both falls under UNIX clones (as does HP-UX, which I just recalled). I can also tell from experience that it works that way; I've worked with them in my old IBM ages.
BalusC
@BalusC - I have no experience with these but a friend of mine worked with some IBM OS that did not have a hierarchical file system. All files were named per convention to emulate directories.
Thomas Jung
@Thomas: thanks for putting in the extra effort in response to my comment. +1.
Grundlefleck
@Grundlefleck - The irony is that I use commons io heavily (mostly http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html) so I was interested what was going on.
Thomas Jung
A: 

Do you have the option of using

System.getProperty("file.separator")

to build the string that represents the path?

Adam
Yes, but I didn't want to build the string. Guessing that this must be a pretty common thing I wanted to reuse an existing snippet of code which did the trick.
Grundlefleck
If you've used the forward slash everywhere you have a path, this seems to work.`String fileSep = System.getProperty("file.separator"); String path = "path/to/some/file"; path.replaceAll("/", fileSep);`
Adam
Yeah, Thomas Jung mentioned that in his answer. Was news to me :)
Grundlefleck
A: 

shouldn't it be enough to say: "path"+File.Seperator+"to"+File.Seperator+"some"+File.Seperator+"file"

Dimitri
As I've mentioned in other comments, yes, I knew a simple string replace strategy would work, but hacking up the string makes the code less readable, and there's a much higher chance for introducing stupid typo errors. Using a solution which was tested, and readable was one of the goals here.
Grundlefleck