28

我开发了许多在 Java 中操作文件的类。我正在开发一个 Linux 机器,并且一直在幸福地输入new File("path/to/some/file");. 到了提交的时候,我意识到该项目的其他一些开发人员正在使用 Windows。我现在想调用一个方法,该方法可以采用字符串形式"/path/to/some/file",并根据操作系统返回正确分隔的路径。

例如:在 Windows 上
"path/to/some/file"变为。 在 Linux 上,它只返回给定的字符串。"path\\to\\some\\file"

我意识到敲出一个可以做到这一点的正则表达式不会花很长时间,但我不想重新发明轮子,而是更喜欢经过适当测试的解决方案。如果它内置在 JDK 中会很好,但如果它是一些小型 F/OSS 库的一部分,那也很好。

那么是否有一个 Java 实用程序可以将字符串路径转换为使用正确的文件分隔符字符?

4

10 回答 10

48

Apache Commons来救援(再次)。该Commons IO方法FilenameUtils.separatorsToSystem(String path)将做你想做的事。

不用说,Apache Commons IO除此之外还会做很多事情,值得一看。

于 2009-11-08T17:53:06.687 回答
18

A"/path/to/some/file"实际上在 Windows Vista 和 XP 下工作。

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

> C:\path\to\some\file

但它仍然不能移植,因为 Windows 有多个。所以必须以某种方式选择根目录。相对路径应该没有问题。

编辑:

Apache commons iounix 和 windows 以外的环境没有帮助。Apache io 源代码

public static String separatorsToSystem(String path) { 
    if (path == null) {
     return null;
    }
    if (isSystemWindows()) {
      return separatorsToWindows(path);
    } else {
      return separatorsToUnix(path);
    }
}
于 2009-11-08T18:01:16.193 回答
8

这就是 Apache commons-io 所做的,展开成几行代码:

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

所以如果你想避免额外的依赖,就使用它。

于 2016-02-26T12:47:31.317 回答
4

在新的 Java 7 中,他们包含了一个名为 Paths 的类,这使您可以完全按照自己的意愿行事(请参阅http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html

这是一个例子:

String rootStorePath = Paths.get("c:/projects/mystuff/").toString();
于 2013-06-03T10:19:46.280 回答
2

您是否可以选择使用

System.getProperty("file.separator")

构建代表路径的字符串?

于 2009-11-08T20:26:03.780 回答
2

对于 7 年后尝试这样做的任何人,apache commons separatorsToSystem 方法已移至 FilenameUtils 类:

FilenameUtils.separatorsToSystem(String path)
于 2016-09-21T18:32:40.187 回答
1
String fileName = Paths.get(fileName).toString();

例如,即使在混合路径下也能与 Windows 完美配合

c:\用户\用户名/我的项目\我的文件/我的文件夹

变成

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

抱歉,还没有检查 Linux 会对上述内容产生什么影响,但 Linux 文件结构再次不同,因此您不会搜索这样的目录

于 2017-11-16T08:32:31.233 回答
1

我创建了这个函数来检查一个字符串是否包含一个\字符,然后将它们转换为/

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

public static String toUrlPath(Path path) {
  return toUrlPath(path.toString());
}
于 2020-01-21T20:02:11.617 回答
0

不应该说:

"path"+File.Seperator+"to"+File.Seperator+"some"+File.Seperator+"file"
于 2009-11-08T21:36:17.827 回答
0

我认为Java路径中有这个漏洞。

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

如果您在具有您需要使用的文件系统的系统上运行它,则可以使用。正如所指出的,它使用了当前的操作系统文件系统。

我需要使用 windows 和 linux 之间的路径,比如将文件从一个复制到另一个。在使用“/”时,我猜如果您使用所有 Java 命令,但我需要进行 sftp 调用,因此使用 / 或file.separatoretc... 对我没有帮助。我无法使用Path(),因为它会将我的文件系统转换为我在“现在”上运行的默认文件系统。

Java需要的是:
在windows系统上:

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

停留c:\home\mystuff并且不会转换为/c:/home/mystuff

类似于使用字符集:

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

PS我也不想加载整个apache io jar文件来做一些简单的事情。在这种情况下,他们无论如何都没有我的建议。

于 2018-03-15T23:08:18.843 回答