开发者

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

开发者 https://www.devze.com 2022-12-10 16:29 出处:网络
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 realis

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开发者_JAVA百科 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?


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

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


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);
    }
}


This is what Apache commons-io does, unrolled into a couple of lines of code:

String separatorsToSystem(String res) {
    if (res==null) return null;
    if (File.separatorChar=='\\') {
        // From Windows to Linux/Mac
        return res.replace('/', File.separatorChar);
    } else {
        // From Linux/Mac to Windows
        return res.replace('\\', File.separatorChar);
    }
}

So if you want to avoid the extra dependency, just use that.


With the new Java 7 they have included a class called Paths this allows you to do exactly what you want (see http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html)

here is an example:

String rootStorePath = Paths.get("c:/projects/mystuff/").toString();


Do you have the option of using

System.getProperty("file.separator")

to build the string that represents the path?


For anyone trying to do this 7 years later, the apache commons separatorsToSystem method has been moved to the FilenameUtils class:

FilenameUtils.separatorsToSystem(String path)


I create this function to check if a String contain a \ character then convert them to /

public static String toUrlPath(String path) {
  return path.indexOf('\\') < 0 ? path : path.replace('\\', '/');
}

public static String toUrlPath(Path path) {
  return toUrlPath(path.toString());
}


String fileName = Paths.get(fileName).toString();

Works perfectly with Windows at least even with mixed paths, for example

c:\users\username/myproject\myfiles/myfolder

becomes

c:\users\username\myproject\myfiles\myfolder

Sorry haven't check what Linux would make of the above but there again Linux file structure is different so you wouldn't search for such a directory


I think there is this hole in Java Paths.

String rootStorePath = Paths.get("c:/projects/mystuff/").toString();

works if you are running it on a system that has the file system you need to use. As pointed out, it used the current OS file system.

I need to work with paths between windows and linux, say to copy a file from one to another. While using "/" every works I guess if you are using all Java commands, but I need to make an sftp call so using / or file.separator etc... does not help me. I cannot use Path() because it converts mine to the default file system I am running on "now".

What Java needs is:
on windows system:

Path posixPath = Paths.get("/home/mystuff", FileSystem.Posix );
stays /home/mystuff/  and does not get converted to \\home\\mystuff

on linux system:
String winPath = Paths.get("c:\home\mystuff", FileSystem.Windows).toString();

stays c:\home\mystuff and does not get converted to /c:/home/mystuff

similar to working with character sets:

URLEncoder.encode( "whatever here", "UTF-8" ).getBytes();

P.S. I also do not want to load a whole apache io jar file to do something simple either. In this case they do not have what I propose anyways.


Shouldn't it be enough to say:

"path"+File.Seperator+"to"+File.Seperator+"some"+File.Seperator+"file"
0

精彩评论

暂无评论...
验证码 换一张
取 消