Java:无论操作系统如何,如何确定路径是否为绝对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1025285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:50:58  来源:igfitidea点击:

Java: How to find out if path is absolute regardless of the OS

javapathabsolute-path

提问by His

Is there anyway in Java to find out if the given path is absolute or not regardless of the platform the program is currently running. So, what I want is probably something like the following example:

无论程序当前运行的平台如何,Java 中是否有任何方法可以确定给定的路径是否为绝对路径。所以,我想要的可能类似于以下示例:

On Linux:

在 Linux 上:

new File("/home/").isAbsolute() // Should return true.
new File("C:/My Documents").isAbsolute() // Should *also* return true.

On Windows:

在 Windows 上:

new File("C:/Documents").isAbsolute() // Should return true.
new File("/home/").isAbsolute() // Should *also* return true.

I can probably code something to get around with this, but I just wanted to find out if anyone knew a built-in class provided in Java to solve this problem. Or has anyone ever come this problem? And how did you solve it?

我可能可以编写一些代码来解决这个问题,但我只是想知道是否有人知道 Java 中提供的内置类来解决这个问题。或者有没有人遇到过这个问题?你是怎么解决的?

Thanks!

谢谢!

采纳答案by Kevin Montrose

Nope.

不。

There are some underlying FileSystemclasses (that's Java 7, but they exist prior to it as well) that expose isAbsolute(), but they're not public - so you shouldn'tuse them, and even if you did your code would be full of reflection junk - and only the "correct" OS ones are included in the JRE, so you'd have to code around them anyway.

有一些底层FileSystem类(即 Java 7,但它们也存在于它之前)公开 isAbsolute(),但它们不是公开的 - 所以你不应该使用它们,即使你做了你的代码充满了反射垃圾 - 只有“正确”的操作系统包含在 JRE 中,因此无论如何您都必须围绕它们进行编码。

Here are the Java 7 implementations of isAbsolute(...) to get you started. Note that File.getPrefixLength() is package-private.

以下是 isAbsolute(...) 的 Java 7 实现,可帮助您入门。请注意 File.getPrefixLength() 是包私有的。

Win32FileSystem:

Win32文件系统

public boolean isAbsolute(File f) 
{
        int pl = f.getPrefixLength();
        return (((pl == 2) && (f.getPath().charAt(0) == slash))
                || (pl == 3));
}

UnixFileSystem:

Unix文件系统

public boolean isAbsolute(File f) 
{
        return (f.getPrefixLength() != 0);
}

回答by Val Blant

In Java 7:

在 Java 7 中:

new File(path).isAbsolute()

回答by remery

My crack at this using Apache FilenameUtil -

我使用 Apache FilenameUtil 破解这个问题 -

   public static boolean isAbsolute(final String path) {
      return FilenameUtils.getPrefixLength(path) != 0;
   }

Technically this is returning !relative. Which is fine for my purposes.

从技术上讲,这是返回 !relative。这对我的目的来说很好。

回答by stuhpa

I ended up using this (in Java 6):

我最终使用了这个(在 Java 6 中):

private static boolean testPath(String path) {
    int prefixLen = FilenameUtils.getPrefixLength(path);
    if (testPathWin(path, prefixLen) || testPathLinux(prefixLen))
        return true;
    else
        return false;
}

private static boolean testPathWin(String path, int prefixLen) {
    if (prefixLen == 3)
        return true;
    File f = new File(path);
    if ((prefixLen == 2) && (f.getPath().charAt(0) == '/'))
        return true;
    return false;
}

private static boolean testPathLinux(int prefixLen) {
    return (prefixLen != 0);
}