java File源码理解,探索File路径
1.方法: new File(path);
我们知道根据输入的路径path的不同 ,File可以根据path的不同格式,来访问文件。那么,path的形式有几种呢?
根据源码



可以知道,输入的路径path其实是在类FileSystem中处理的。FileSystem是一个抽象类,所以,其实是在其实现类WinNTFileSystem中处理。
设定一个目标,我们要得到文件的绝对地址!
由如下代码

可以看出,我们是从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 < n; i++) {
char c = path.charAt(i);
if (c == altSlash) // 1:包含“/”
return normalize(path, n, (prev == slash) ? i - 1 : i);
if ((c == slash) && (prev == slash) && (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 < 3) off = 0; /* Avoid fencepost cases with UNC pathnames */
int src;
char slash = this.slash;
StringBuffer sb = new StringBuffer(len); if (off == 0) {
/* Complete normalization, including prefix */
src = normalizePrefix(path, len, sb);
} else {
/* Partial normalization */
src = off;
sb.append(path.substring(0, off));
} /* Remove redundant slashes from the remainder of the path, forcing all
slashes into the preferred slash */
while (src < len) {
char c = path.charAt(src++);
if (isSlash(c)) {
while ((src < len) && isSlash(path.charAt(src))) src++;
if (src == len) {
/* Check for trailing separator */
int sn = sb.length();
if ((sn == 2) && (sb.charAt(1) == ':')) {
/* "z:\\" */
sb.append(slash);
break;
}
if (sn == 0) {
/* "\\" */
sb.append(slash);
break;
}
if ((sn == 1) && (isSlash(sb.charAt(0)))) {
/* "\\\\" is not collapsed to "\\" because "\\\\" marks
the beginning of a UNC pathname. Even though it is
not, by itself, a valid UNC pathname, we leave it as
is in order to be consistent with the win32 APIs,
which treat this case as an invalid UNC pathname
rather than as an alias for the root directory of
the current drive. */
sb.append(slash);
break;
}
/* Path does not denote a root directory, so do not append
trailing slash */
break;
} else {
sb.append(slash);
}
} else {
sb.append(c);
}
} String rv = sb.toString();
return rv;
}
路径类型的分类:
@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 及其 绝对路径
| 输入的path | /long | lo/ng | \/long | long/k | long | D:\JACK\OK | \\long(或者\long) | //long |
| this.path | \long | lo\ng | X非法输入 | long\k | long | D:\JACK\OK | \long | \\long |
| this.PrefixLength | 1 | 0 | X | 0 | 0 | 3 | 1 | 2 |
| 相关类型 | 盘相关 | 项目路径相关 | 项目路径相关 | 项目路径相关 | 完全绝对路径 | 盘相关 | ||
| absolutePath | D:\long | D:\javaTest\long | X | D:\javaTest\long\k | D:\javaTest\long | D:\JACK\OK | D:\long | \\long |
总结: 1:输入path 以'/' 或者 ’\\‘ 开头的 ,是以项目所在的硬盘位基础路径
2:输入path 以 字母开头 的,是以项目的路径为基础路径 即: System.getProperty("user.dir")
3.输入绝对路径的,就是以该绝对路径做为路径咯
java File源码理解,探索File路径的更多相关文章
- java io 源码研究记录(一)
Java IO 源码研究: 一.输入流 1 基类 InputStream 简介: 这是Java中所有输入流的基类,它是一个抽象类,下面我们简单来了解一下它的基本方法和抽象方法. 基本方法: publ ...
- 编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码
转自:编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码 JDK:java version “1.8.0_31”Java(TM) SE Runtime Environment ( ...
- 基于SpringBoot的Environment源码理解实现分散配置
前提 org.springframework.core.env.Environment是当前应用运行环境的公开接口,主要包括应用程序运行环境的两个关键方面:配置文件(profiles)和属性.Envi ...
- 深入源码理解Spring整合MyBatis原理
写在前面 聊一聊MyBatis的核心概念.Spring相关的核心内容,主要结合源码理解Spring是如何整合MyBatis的.(结合右侧目录了解吧) MyBatis相关核心概念粗略回顾 SqlSess ...
- Java集合源码分析(三)LinkedList
LinkedList简介 LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈.队列和双端队列来使用. LinkedList同样是非线程安全 ...
- 【转】Java HashMap 源码解析(好文章)
.fluid-width-video-wrapper { width: 100%; position: relative; padding: 0; } .fluid-width-video-wra ...
- Eclipse Java 关联源码
今天打代码的时候打算看看Java的源码是怎么实现的 没想到还没关联源码 遇到上面的情况只需要关联下源码就可以对着方法按F3查看JAVA的开源代码. 解决上面如下: 找到jdk的安装目录 找到src.z ...
- 转:【Java集合源码剖析】LinkedHashmap源码剖析
转载请注明出处:http://blog.csdn.net/ns_code/article/details/37867985 前言:有网友建议分析下LinkedHashMap的源码,于是花了一晚上时 ...
- 自学Java HashMap源码
自学Java HashMap源码 参考:http://zhangshixi.iteye.com/blog/672697 HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提 ...
随机推荐
- jeecg uedit 自定义图片上传路径
jeecg uedit 图片上传配置自定义物理路径,简单描述:我们知道 jeecg 中使用的 uedit 默认图片上传路径为 "当前项目\plug-in\ueditor\jsp\upload ...
- (4)ardunio 矩阵求解官方库改造,添加逆的求解
多此一举,原来官方库给了求逆的函数,在源码里 除此之外,还有转置矩阵,只不过样例没显示出来. //Matrix Inversion Routine // * This function inverts ...
- 如何用okr做好目标规划
有朋友和我吐槽公司总是规划一个个振奋人心的目标,让大家对工作充满了热情.然而好的开头却缺少追踪反馈没有好的结尾,那些大家所渴望达成的目标随着时间的流逝便逐渐没有了音信,不再有人主动提起,团队成员迎来的 ...
- 权限管理(chown、chgrp、umask)
对于文件或目录的权限的修改,只能管理员和文件的所有者拥有此权限,但是对于文件或目录的的所有者的更改,只有管理员拥有此权限(虽然普通用户创建的文件或目录,用户也不能修改文件或目录的所有者). 1.cho ...
- 使用readthedocs 发布 sphinx doc文档
readthedocs 是由社区驱动的开源sphinx doc 托管服务,我们可以用来方便的构建以及发布文档 这是一个简单的demo 项目,使用了用的比较多的sphinx_rtd_theme 主题,主 ...
- ipcs
用于报告Linux中进程间通信设施的状态,显示的信息包括消息列表.共享内存和信号量的信息
- 【luoguP4544】[USACO10NOV]购买饲料Buying Feed
题目链接 首先把商店按坐标排序 \(dp_{i,j}\)表示前i个商店买了j吨饲料并运到终点的花费,二进制拆分优化转移 #include<algorithm> #include<io ...
- 原创:logistic regression实战(一):SGD Without lasso
logistic regression是分类算法中非常重要的算法,也是非常基础的算法.logistic regression从整体上考虑样本预测的精度,用判别学习模型的条件似然进行参数估计,假设样本遵 ...
- IDEA 重新 build Project
- 块元素&行内元素
大多数HTML 元素被定义为块级元素或内联元素.块级元素在浏览器显示时,通常会以新行来开始(和结束) block元素特点 1 总是在新行上开始: 2 高度,行高以及外边距和内边距都可控制: 3 宽度缺 ...