file的这几个取得path的方法各有不同,下边说说详细的区别
html, body {
font-size: 15px;
}
body {
font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, "Microsoft YaHei UI", SimSun, SimHei, arial, sans-serif;
line-height: 1.6;
color: ;
background-color: ;
margin: 0;
padding: 16px 20px;
}
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
margin: 1.33rem 0 0.667rem;
padding: 0;
font-weight: bold;
}
h1 {
font-size: 21px;
font-size: 1.4rem;
}
h2 {
font-size: 20px;
font-size: 1.33rem;
}
h3 {
font-size: 18px;
font-size: 1.2rem;
}
h4 {
font-size: 17px;
font-size: 1.13rem;
}
h5 {
font-size: 15px;
font-size: 1rem;
}
h6 {
font-size: 15px;
font-size: 1rem;
color: #777777;
margin: 1rem 0;
}
div, p, ul, ol, dl, li {
margin: 0;
}
blockquote, table, pre, code{
margin: 8px 0;
}
ul, ol {
padding-left: 32px;
padding-left: 2.13rem;
}
blockquote {
border-left: 4px solid #dddddd;
padding: 0 12px;
padding: 0 0.8rem;
}
blockquote > :first-child {
margin-top: 0;
}
blockquote > :last-child {
margin-bottom: 0;
}
img {
border: 0;
max-width: 100%;
height: auto !important;
margin: 2px 0;
}
table {
border-collapse: collapse;
border: 1px solid #bbbbbb;
}
td {
padding:4px 8px;
border-collapse: collapse;
border: 1px solid #bbbbbb;
}
@media screen and (max-width: 660px) {
body {
padding: 20px 18px;
padding: 1.33rem 1.2rem;
}
}
@media only screen and (-webkit-max-device-width: 1024px), only screen and (-o-max-device-width: 1024px), only screen and (max-device-width: 1024px), only screen and (-webkit-min-device-pixel-ratio: 3), only screen and (-o-min-device-pixel-ratio: 3), only screen and (min-device-pixel-ratio: 3) {
html, body {
font-size: 17px;
}
body {
line-height: 1.7;
padding: 0.75rem 0.9375rem;
color: #353c47;
}
h1 {
font-size: 2.125rem;
}
h2 {
font-size: 1.875rem;
}
h3 {
font-size: 1.625rem;
}
h4 {
font-size: 1.375rem;
}
h5 {
font-size: 1.125rem;
}
h6 {
color: inherit;
}
ul, ol {
padding-left: 2.5rem;
}
blockquote {
padding: 0 0.9375rem;
}
}
file的这几个取得path的方法各有不同,下边说说详细的区别
概念上的区别:(内容来自jdk,个人感觉这个描述信息,只能让明白的人明白,不明白的人看起来还是有点难度(特别试中文版,英文版稍好些)所以在概念之后我会举例说明。如果感觉看概念很累就跳过直接看例子吧。看完例子回来看概念会好些。
public String getPath()将此抽象路径名转换为一个路径名字符串。所得到的字符串使用默认名称分隔符来分隔名称序列中的名称。
返回:
此抽象路径名的字符串形式
public String getAbsolutePath()返回抽象路径名的绝对路径名字符串。
如 果此抽象路径名已经是绝对路径名,则返回该路径名字符串,这与 getPath() 方法一样。如果此抽象路径名是空的抽象路径名,则返回当前用户目录的 路径名字符串,该目录由系统属性 user.dir 指定。否则,使用与系统有关的方式分析此路径名。在 UNIX 系统上,通过根据当前用户目录分析某 一相对路径名,可使该路径名成为绝对路径名。在 Microsoft Windows 系统上,通过由路径名指定的当前驱动器目录(如果有)来分析某一相 对路径名,可使该路径名成为绝对路径名;否则,可以根据当前用户目录来分析它。
返回:
绝对路径名字符串,它与此抽象路径名表示相同的文件或目录的
抛出:
SecurityException - 如果无法访问所需的系统属性值。
另请参见:
isAbsolute()
public String getCanonicalPath()
throws IOException返回抽象路径名的规范路径名字符串。
规 范路径名是绝对路径名,并且是惟一的。规范路径名的准确定义与系统有关。如有必要,此方法首先将路径名转换成绝对路径名,这与调 用 getAbsolutePath() 方法的效果一样,然后用与系统相关的方式将它映射到其惟一路径名。这通常涉及到从路径名中移除多余的名称(比 如 "." 和 "..")、分析符号连接(对于 UNIX 平台),以及将驱动器名转换成标准大小写形式(对于 Microsoft Windows 平台)。
表 示现有文件或目录的每个路径名都有一个惟一的规范形式。表示非存在文件或目录的每个路径名也有一个惟一的规范形式。非存在文件或目录路径名的规范形式可能 不同于创建文件或目录之后同一路径名的规范形式。同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。
返回:
表示与此抽象路径名相同的文件或目录的规范路径名字符串
抛出:
IOException - 如果发生 I/O 错误(可能是因为构造规范路径名需要进行文件系统查询)
SecurityException - 如果无法访问所需的系统属性值,或者存在安全管理器,且其 SecurityManager.checkRead(java.io.FileDescriptor) 方法拒绝对该文件进行读取访问
从以下版本开始:
JDK1.1
二、例子:
1,getPath()与getAbsolutePath()的区别
File file1 = new File(".\\test1.txt");
File file2 = new File("D:\\workspace\\test\\test1.txt");
System.out.println("-----默认相对路径:取得路径不同------");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println("-----默认绝对路径:取得路径相同------");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
}
得到的结果:
.\test1.txt
D:\workspace\test\.\test1.txt
-----默认绝对路径:取得路径相同------
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt
因为getPath()得到的是构造file的时候的路径。
getAbsolutePath()得到的是全路径
如果构造的时候就是全路径那直接返回全路径
如果构造的时候试相对路径,返回当前目录的路径+构造file时候的路径
2,getAbsolutePath()和getCanonicalPath()的不同
File file = new File("..\\src\\test1.txt");
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
}
得到的结果
D:\workspace\src\test1.txt
可以看到CanonicalPath不但是全路径,而且把..或者.这样的符号解析出来。
3,getCanonicalPath()和自己的不同。
就是解释这段话:
单下边这段代码是看不到结果的,要配合一定的操作来看。下边操作步骤,同时讲解
File file = new File("D:\\Text.txt");
System.out.println(file.getCanonicalPath());
}
步骤:
确定你的系统是Windows系统。
(1),确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:
注意这里试大写的Text.txt
(2)在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果
同样的代码得到不同的结果。
同时可以对比getAbsolutePath()看看,这个得到的结果是一样的。
原因:
window 是大小写不敏感的,也就是说在windows上test.txt和Test.txt是一个文件,所以在windows上当文件不存在时,得到的路径就是按 照输入的路径。但当文件存在时,就会按照实际的情况来显示。这也就是建立文件后和删除文件后会有不同的原因。文件夹和文件类似。
三、最后:
1,尝试在linux下执行上边的步骤,两次打印的结果是相同的,因为linux是大小写敏感的系统。
2,手动删掉test.txt,然后尝试执行下边代码
File file = new File("D:\\Text.txt");
System.out.println(file.getCanonicalPath());
File file1 = new File("D:\\text.txt");
file1.createNewFile();
file = new File("D:\\Text.txt");
System.out.println(file.getCanonicalPath());
}
File file1 = new File("D:\\text.txt");
file1.createNewFile();
File file = new File("D:\\Text.txt");
System.out.println(file.getCanonicalPath());
}
执行上边两个函数,看看结果,然后思考一下为什么?
1,的结果是两个大写,
2,的结果试两个小写
连续两个大写的,是否跟上边的矛盾 ?
这是因为虚拟机的缓存机制造成的。第一次File file = new File("D:\\Text.txt");决定了结果.
file的这几个取得path的方法各有不同,下边说说详细的区别的更多相关文章
- open_basedir restriction in effect. File() is not within the allowed path(s)
目前发现eaccelerator安装之后如果php.ini中设置open_basedir将导致open_basedir的一些报错(open_basedir restriction in effect. ...
- More than one file was found with OS independent path 錯誤
More than one file was found with OS independent path 'lib/armeabi/libmrpoid.so',. 翻譯過來就是:在操作系統的獨立目錄 ...
- More than one file was found with OS independent path 'META-INF/LICENSE' | Error:Could not read \build\intermediates\typedefs.txt (系统找不到指定的文件。)
FAQ1: Error:Could not read E:\new\PlatformLibrary\CommonLibrary\build\intermediates\typedefs.txt: E: ...
- 报错:Can't find a source file at "xxxxx“Locate the file or edit the source lookup path to include its location.
调试问题: Can't find a source file at "/tmp/TI_MKLIB6sLCzz/SRC/exit.c" Locate the file or edit ...
- android异常 More than one file was found with OS independent path 'META-INF/XXX'
android 异常总结:一个文件在jar包中出现多次. Error:Execution failed for task ':app:transformResourcesWithMergeJavaRe ...
- More than one file was found with OS independent path 'lib/armeabi-v7a/libgnustl_shared.so'
More than one file was found with OS independent path 'xxx/xxx' 这个错误是在路径中出现了重复依赖. 解决办法是配置打包选项, 在 and ...
- python3 os.path.realpath(__file__) 和 os.path.cwd() 方法的区别
python3 os.path.realpath(__file__) 和 os.path.cwd() 方法的区别 os.path.realpath 获取当前执行脚本的绝对路径. os.path.rea ...
- Linux遇到的问题(一)Ubuntu报“xxx is not in the sudoers file.This incident will be reported” 错误解决方法
提示错误信息 www@iZ236j3sofdZ:~$ ifconfig Command 'ifconfig' is available in '/sbin/ifconfig' The command ...
- Message: 'geckodriver' executable needs to be in PATH. 解决方法
问题描述: 执行如下代码 # coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.maxim ...
随机推荐
- 理解Linux系统负荷[转]
一.查看系统负荷 在Linux系统中,我们一般使用uptime命令查看(w命令和top命令也行).(另外,它们在苹果公司的Mac电脑上也适用.) 二.一个类比 我们不妨把这个CPU想象成一座大桥, ...
- LCIS(m*n) 最长公共上升子序列
详见:http://wenku.baidu.com/view/3e78f223aaea998fcc220ea0n3的: for(int i=1;i<=n;i++) for ...
- ”sql Server2008 应用程序无法启动,因为应用程序的并行配置不正确。 找不到从属程序集。“C:\windows\SysWOW64\DTSPipelinePerf100.dll”的激活上下文生成失败“的解决方案
一:控制面板->管理工具->事件查看器->windows日志->应用程序 查看错误原因: 二:在其他机子上拷贝一个DTSWizard.exe.config文件替换本机上已经 ...
- 采用Asp.Net的Forms身份验证时,持久Cookie的过期时间会自动扩展
原文:http://www.cnblogs.com/sanshi/archive/2012/06/22/2558476.html 若是持久Cookie,Cookie的有效期Expiration属性有当 ...
- HDU4010 Query on The Trees(LCT)
人生的第一道动态树,为了弄懂它的大致原理,需要具备一些前置技能,如Splay树,树链剖分的一些概念.在这里写下一些看各种论文时候的心得,下面的代码是拷贝的CLJ的模板,别人写的模板比较可靠也方便自己学 ...
- POJ 2021 Relative Relatives(map+树的遍历)
题意: 今天是Ted的100岁生日.凑巧的是,他家族里面每个人都跟他同一天生日,但是年份不同. 现在只给出一些 父亲的名字,孩子的名字,以及孩子出生时父亲的年龄, 要求将Ted以外的家族成员按年龄降序 ...
- POJ 1496
#include <iostream> #include <string> using namespace std; int fac(int num); int C(int n ...
- DevExpress licenses.licx 问题
在DevExpress ( 当然并不范指DevExpress,很多收费软件都是这样的)中,licenses.licx 是用户许可证书文件,当我们使用某些ActiveX(是Microsoft对于一系列策 ...
- 取得DisplayMerics手机屏幕大小的应用
DisplayMerics:A structure describing general information about a display, such as its size, density, ...
- Windows服务创建及安装
Windows服务创建及安装http://www.cnblogs.com/tuyile006/archive/2006/11/27/573654.html