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

荒凉一梦 提交于 2019-11-28 23:08:26
Brian Agnew

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

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)
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.

Dimitri

Shouldn't it be enough to say:

"path"+File.Seperator+"to"+File.Seperator+"some"+File.Seperator+"file"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!