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

发布于 2024-07-25 07:37:55 字数 508 浏览 5 评论 0原文

不管程序当前运行的平台如何,Java 中是否有办法找出给定路径是否是绝对路径。 所以,我想要的可能是像下面的例子:

在Linux上:

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

在Windows上:

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

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

谢谢!

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:

On Linux:

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

On 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?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

银河中√捞星星 2024-08-01 07:37:55

没有。

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

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

Win32FileSystem

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

UnixFileSystem

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

Nope.

There are some underlying FileSystem classes (that's Java 7, but they exist prior to it as well) that expose isAbsolute(), but they're not public - so you shouldn't use 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.

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

Win32FileSystem:

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

UnixFileSystem:

public boolean isAbsolute(File f) 
{
        return (f.getPrefixLength() != 0);
}
韬韬不绝 2024-08-01 07:37:55

在 Java 7 中:

new File(path).isAbsolute()

In Java 7:

new File(path).isAbsolute()
空气里的味道 2024-08-01 07:37:55

我使用 Apache FilenameUtil 对此进行了破解 -

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

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

My crack at this using 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.

谷夏 2024-08-01 07:37:55

我最终使用了这个(在 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);
}

I ended up using this (in 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文