gpt4 book ai didi

java - 为什么 `Paths.get` 方法的行为不同?

转载 作者:行者123 更新时间:2023-12-04 11:02:17 32 4
gpt4 key购买 nike

此代码片段打印出来 \\home\cay\

Path absolute = Paths.get("/", "home", "cay");
System.out.println(absolute);

然而这个打印出来 myprog\conf\user.properties
Path relative = Paths.get("myprog", "conf", "user.properties");
System.out.println(relative);

我有两个问题:
  • 为什么有两个领先\\在第一种情况下?
  • 为什么会有尾随 \在第一种情况下?

  • ps:我在Windows系统上。

    最佳答案

    您的第一个路径被解释为 UNC path没有对象名称。

    怎么发生的?
    深入实现,我们可以发现以下内容:

    sun.nio.fs.WindowsPathParser

    /**
    * Parses the given input as a Windows path.
    *
    * @param input - 1st case: '/\home\cay' ; 2nd case: 'myprog\conf\user.properties'
    * @param requireToNormalize - true for both cases
    */
    private static Result parse(String input, boolean requireToNormalize) {
    String root = "";
    WindowsPathType type = null;

    int len = input.length(); // 1st case: len = 10 , 2nd case: len = 27
    int off = 0;
    if (len > 1) {
    char c0 = input.charAt(0); // 1st case: c0 = '/' , 2nd case: c0 = 'm'
    char c1 = input.charAt(1); // 1st case: c1 = '\' , 2nd case: c1 = 'y'
    int next = 2;
    if (isSlash(c0) && isSlash(c1)) // this condition is true ONLY for 1st case
    {
    type = WindowsPathType.UNC;
    off = nextNonSlash(input, next, len); // 1st case: off = 2
    next = nextSlash(input, off, len); // 1st case: next = 6
    if (off == next)
    throw new InvalidPathException(input, "UNC path is missing hostname");

    String host = input.substring(off, next); // 1st case: host = home
    off = nextNonSlash(input, next, len); // 1st case: off = 7
    next = nextSlash(input, off, len); // 1st case: next = 10
    if (off == next)
    throw new InvalidPathException(input, "UNC path is missing sharename");

    root = "\\\\" + host + "\\" + input.substring(off, next) + "\\"; // 1st case: root = \\home\cay\
    off = next; // 1st case: off = next = 10
    } else {
    if (isLetter(c0) && c1 == ':') { // this condition is false for 2nd case
    ...
    }
    }
    }
    if (off == 0) { // 1st case: off = 10, 2nd case: off = 0
    if (len > 0 && isSlash(input.charAt(0))) { // 2nd case: false
    type = WindowsPathType.DIRECTORY_RELATIVE;
    root = "\\";
    } else {
    type = WindowsPathType.RELATIVE;
    }
    }

    if (requireToNormalize) { // true for both cases
    StringBuilder sb = new StringBuilder(input.length());
    sb.append(root);
    /*
    1st case: type = UNC, root = '\\home\cay\' , off = 10 , normalize(...) = '\\home\cay\'
    2nd case: type = RELATIVE, root = '' , off = 0 , normalize(...) = 'myprog\conf\user.properties'
    */
    return new Result(type, root, normalize(sb, input, off));
    } else {
    ...
    }
    }

    private static final boolean isSlash(char c) {
    return (c == '\\') || (c == '/');
    }

    sun.nio.fs.WindowsPathType

    因此,对于第一种情况,您将拥有 UNC 路径类型,对于第二种情况, 亲戚路径类型。

    如果您尝试运行以下代码段,您将得到稍微不同的结果(没有最终的 \ ,因为 对象名称 将出现):

    Path uncPath = Paths.get("/", "my-host", "share", "obj");
    System.out.println(uncPath); // \\my-host\share\obj

    如果您尝试运行以下代码段:

    Path uncPath = Paths.get("/", "my-host");

    你会得到:
    java.nio.file.InvalidPathException: UNC path is missing sharename: /\my-host
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:118)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
    at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
    at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
    at java.nio.file.Paths.get(Paths.java:84)

    因为根据 this :

    In a UNC path used to access files and directories in an SMB share, for example, object-name can be the name of a file or a directory. The host-name, share-name, and object-name are referred to as "pathname components" or "path components". A valid UNC path consists of two or more path components.

    关于java - 为什么 `Paths.get` 方法的行为不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58728440/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com