欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  类库下载

java File源码理解,探索File路径

程序员文章站 2022-03-21 19:15:05
...
1.方法: new File(path);

我们知道根据输入的路径path的不同 ,File可以根据path的不同格式,来访问文件。那么,path的形式有几种呢?

根据源码

java File源码理解,探索File路径

java File源码理解,探索File路径

java File源码理解,探索File路径

可以看出,我们是从WinNTFileSystem 的resolve得到绝对路径

@Override
    public String resolve(File f) {
        String path = f.getPath();
        int pl = f.getPrefixLength();
        if ((pl == 2) && (path.charAt(0) == slash))
            return path;                        /* UNC */
        if (pl == 3)
            return path;                        /* Absolute local */
        if (pl == 0)
            return getUserPath() + slashify(path); /* Completely relative */
        if (pl == 1) {                          /* Drive-relative */
            String up = getUserPath();
            String ud = getDrive(up);
            if (ud != null) return ud + path;
            return up + path;                   /* User dir is a UNC path */
        }
        if (pl == 2) {                          /* Directory-relative */
            String up = getUserPath();
            String ud = getDrive(up);
            if ((ud != null) && path.startsWith(ud))
                return up + slashify(path.substring(2));
            char drive = path.charAt(0);
            String dir = getDriveDirectory(drive);
            String np;
            if (dir != null) {
                /* When resolving a directory-relative path that refers to a
                   drive other than the current drive, insist that the caller
                   have read permission on the result */
                String p = drive + (':' + dir + slashify(path.substring(2)));
                SecurityManager security = System.getSecurityManager();
                try {
                    if (security != null) security.checkRead(p);
                } catch (SecurityException x) {
                    /* Don't disclose the drive's directory in the exception */
                    throw new SecurityException("Cannot resolve path " + path);
                }
                return p;
            }
            return drive + ":" + slashify(path.substring(2)); /* fake it */
        }
        throw new InternalError("Unresolvable path: " + path);
    }

我们需要两个参数,即String path = f.getPath();和int pl = f.getPrefixLength();

得到path

注意:windows 把"\\"当成"\"处理,即 "\\".length() ==1

@Override
    public String normalize(String path) {
        int n = path.length();
        char slash = this.slash;
        char altSlash = this.altSlash;
        char prev = 0;
        for (int i = 0; i  1)) /:2:包含  "\",并且"\\",并且i>1
                return normalize(path, n, i - 1);
            if ((c == ':') && (i > 1))   //3:包含 ":",并且i>1
                return normalize(path, n, 0);
            prev = c;
        }
        if (prev == slash) return normalize(path, n, n - 1);
        return path;  //4:若是绝对路径,则直接返回绝对路径
                      //5:"\\" 或 "\" 开头,直接返回
    }
/* Normalize the given pathname, whose length is len, starting at the given
       offset; everything before this offset is already normal. */
    private String normalize(String path, int len, int off) {
        if (len == 0) return path;
        if (off 

路径类型的分类:

@Override
    public int prefixLength(String path) {
        char slash = this.slash;
        int n = path.length();
        if (n == 0) return 0;
        char c0 = path.charAt(0);
        char c1 = (n > 1) ? path.charAt(1) : 0;
        if (c0 == slash) {
            if (c1 == slash) return 2;  /* Absolute UNC pathname "\\\\foo" */ UNC的绝对路径
            return 1;                   /* Drive-relative "\\foo" */ 与驱动盘相对路径
        }
        if (isLetter(c0) && (c1 == ':')) {
            if ((n > 2) && (path.charAt(2) == slash))
                return 3;               /* Absolute local pathname "z:\\foo" */  本地绝对路径
            return 2;                   /* Directory-relative "z:foo" */  目录相对路径
        }
        return 0;                       /* Completely relative */   相对路径
    }

File(path)各种输入的path 及其 绝对路径

java File源码理解,探索File路径

总结: 1:输入path 以'/' 或者 ’\\‘ 开头的 ,是以项目所在的硬盘位基础路径

2:输入path 以 字母开头 的,是以项目的路径为基础路径 即: System.getProperty("user.dir")

3.输入绝对路径的,就是以该绝对路径做为路径咯

相关标签: java File