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;
}
}

概念上的区别:(内容来自jdk,个人感觉这个描述信息,只能让明白的人明白,不明白的人看起来还是有点难度(特别试中文版,英文版稍好些)所以在概念之后我会举例说明。如果感觉看概念很累就跳过直接看例子吧。看完例子回来看概念会好些。

getPath
public?String?getPath()将此抽象路径名转换为一个路径名字符串。所得到的字符串使用默认名称分隔符来分隔名称序列中的名称。?

返回:
此抽象路径名的字符串形式

getAbsolutePath
public?String?getAbsolutePath()返回抽象路径名的绝对路径名字符串。?
如 果此抽象路径名已经是绝对路径名,则返回该路径名字符串,这与?getPath()?方法一样。如果此抽象路径名是空的抽象路径名,则返回当前用户目录的 路径名字符串,该目录由系统属性?user.dir?指定。否则,使用与系统有关的方式分析此路径名。在?UNIX?系统上,通过根据当前用户目录分析某 一相对路径名,可使该路径名成为绝对路径名。在?Microsoft?Windows?系统上,通过由路径名指定的当前驱动器目录(如果有)来分析某一相 对路径名,可使该路径名成为绝对路径名;否则,可以根据当前用户目录来分析它。?

返回:
绝对路径名字符串,它与此抽象路径名表示相同的文件或目录的?
抛出:?
SecurityException?-?如果无法访问所需的系统属性值。
另请参见:
isAbsolute()

getCanonicalPath
public?String?getCanonicalPath()
????????????????????????throws?IOException返回抽象路径名的规范路径名字符串。?
规 范路径名是绝对路径名,并且是惟一的。规范路径名的准确定义与系统有关。如有必要,此方法首先将路径名转换成绝对路径名,这与调 用?getAbsolutePath()?方法的效果一样,然后用与系统相关的方式将它映射到其惟一路径名。这通常涉及到从路径名中移除多余的名称(比 如?"."?和?"..")、分析符号连接(对于?UNIX?平台),以及将驱动器名转换成标准大小写形式(对于?Microsoft?Windows?平台)。?

表 示现有文件或目录的每个路径名都有一个惟一的规范形式。表示非存在文件或目录的每个路径名也有一个惟一的规范形式。非存在文件或目录路径名的规范形式可能 不同于创建文件或目录之后同一路径名的规范形式。同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。?

返回:
表示与此抽象路径名相同的文件或目录的规范路径名字符串?
抛出:?
IOException?-?如果发生?I/O?错误(可能是因为构造规范路径名需要进行文件系统查询)?
SecurityException?-?如果无法访问所需的系统属性值,或者存在安全管理器,且其?SecurityManager.checkRead(java.io.FileDescriptor)?方法拒绝对该文件进行读取访问
从以下版本开始:?
JDK1.1?

二、例子:
1,getPath()与getAbsolutePath()的区别

public?static?void?test1(){
????????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()的不同

public?static?void?test2()?throws?Exception{
????????File?file?=?new?File("..\\src\\test1.txt");
????????System.out.println(file.getAbsolutePath());
????????System.out.println(file.getCanonicalPath());
????}

得到的结果

D:\workspace\test\..\src\test1.txt
D:\workspace\src\test1.txt

可以看到CanonicalPath不但是全路径,而且把..或者.这样的符号解析出来。
3,getCanonicalPath()和自己的不同。
就是解释这段话:

表 示现有文件或目录的每个路径名都有一个惟一的规范形式。表示非存在文件或目录的每个路径名也有一个惟一的规范形式。非存在文件或目录路径名的规范形式可能 不同于创建文件或目录之后同一路径名的规范形式。同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。?

单下边这段代码是看不到结果的,要配合一定的操作来看。下边操作步骤,同时讲解

public?static?void?test3()?throws?Exception{
????????File?file?=?new?File("D:\\Text.txt");
????????System.out.println(file.getCanonicalPath());
????}

步骤:
确定你的系统是Windows系统。
(1),确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:

D:\Text.txt

注意这里试大写的Text.txt
(2)在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果

D:\text.txt

同样的代码得到不同的结果。
同时可以对比getAbsolutePath()看看,这个得到的结果是一样的。

原因:
window 是大小写不敏感的,也就是说在windows上test.txt和Test.txt是一个文件,所以在windows上当文件不存在时,得到的路径就是按 照输入的路径。但当文件存在时,就会按照实际的情况来显示。这也就是建立文件后和删除文件后会有不同的原因。文件夹和文件类似。

三、最后:
1,尝试在linux下执行上边的步骤,两次打印的结果是相同的,因为linux是大小写敏感的系统。
2,手动删掉test.txt,然后尝试执行下边代码

public?static?void?test4()?throws?Exception{
????????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());
????}

public?static?void?test3()?throws?Exception{
????????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");决定了结果.

来自为知笔记(Wiz)

时间: 2024-08-29 04:42:24

file的这几个取得path的方法各有不同,下边说说详细的区别的相关文章

More than one file was found with OS independent path 'lib/arm64-v8a/libsqlite.so'

很明显的意思是找到了多个so库.那么就需要去掉重复的. 在app build.gradle中配置如下: packagingOptions { pickFirst 'lib/arm64-v8a/libsqlite.so' pickFirst 'lib/armeabi-v7a/libsqlite.so' pickFirst 'lib/armeabi/libsqlite.so' pickFirst 'lib/x86/libsqlite.so' } 解决地址:https://github.com/car

centos添加和删除用户及 xxx is not in the sudoers file.This incident will be reported.的解决方法

1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加一个名为tommy的用户 #passwd tommy   //修改密码 Changing password for user tommy. New UNIX password:     //在这里输入新密码 Retype new UNIX password:  //再次输入新密码 passwd: all authentication tokens updated successfully.

nvcc fatal : Cannot find compiler 'cl.exe' in PATH解决方法

我在测试安装的deep learning工具theano.按照官网Baby Steps - Algebra一步步输入. >>> import theano.tensor as T >>> from theano import function >>> x = T.dscalar('x') >>> y = T.dscalar('y') >>> z = x + y >>> f = function([x

一键ghost提示“Cannot open image file'1.4 I:/~1/c_pan.gho'”解决方法

出现了这提示的话,估计你是用U盘安装,或者是曾经做过分区操作(如把H盘改为D盘) 如果你用旧版的ghost的话,估计I:/~1/c_pan.gho已经被自动删了,那么你只能下载数据恢复工具(如"易我数据恢复工具")把镜像恢复回来. 如果镜像还在的话: [失败原因]"Cannot open image file'1.4 I:/~1/c_pan.gho'"中的1.4指的是第一个硬盘的第四个分区,并不是真正的 I 盘,搜索的地方错了,自然搜不到. [解决方法] 1.进入中

sublime text3 取消显示路径path的方法

sublime text3 每当运行报错error时,都会出现一长串的path路径,如何不显示呢? 可以通过注释掉Packages/Default/exec.py的四个特定行来更改. 首先,您需要从Package Control中安装PackageResourceViewer插件. 接下来,打开命令调色板Ctrl+Shift+P,键入prv,以显示PackageResourceViewer选项. 选择PackageResourceViewer: Open Resource 然后选择Default

Linux添加环境变量到PATH的方法

Centos Linux添加环境变量到PATH的方法 在 Linux CentOS 系统上安装完 Hadoop 后,为了使用方便,需要将 Hadoop 命令加到系统命令中,如果在没有添加到环境变量之前,执行“hadoop fs”命令时,则会提示命令不存在的错误,如下所示: 方法一1. export PATH="$PATH:/my_new_path" 直接运行命令,使用这种方法,只会对当前回话生效,也就是说每当登出或注销系统以后,新export的变量就会失效,还是恢复为原来profile

安卓初級教程(2):SD創建file,儲存與讀寫的方法(1)

package com.sdmadik; import java.io.*; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.view.View.OnClickListener; import android.widget.*; public class FileUse extends Activity { // GUI controls EditText t

cvs或svn上下载的新项目无build path 解决方法

工程名右键无build path, 右键propertise中也无.列出两个解决方法. 1  .classpath 文件缺失 或者 .project 文件缺失,可以直接在其他可以build的工程中复制并且改一下 打开 .classpath 更改工程名. 新建 .project 复制黏贴一下代码 <?xml version="1.0" encoding="UTF-8"?> <projectDescription> <name>你的工

linux下vi或vim编辑文件时提示Found a swap file by the name的原因及解决方法

在linux下用vi或vim打开test.java文件时 [[email protected] test]# vi test.java 出现了如下信息: E325: ATTENTION     Found a swap file by the name ".test.java.swp"             owned by: root   dated: Wed Dec  7 13:52:56 2011         file name: /var/tmp/Test.java