`

file getPath getAbsolutePath getCanonicalPath不同(z)

 
阅读更多

file getPath getAbsolutePath getCanonicalPath不同

 

例子:
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");决定了结果.

分享到:
评论

相关推荐

    java File类的一些知识

    File的getPath_getAbsolutePath和getCanonicalPath的不同

    Java中getPath与getAbsolutePath区别.pdf

    Java中getPath与getAbsolutePath区别.pdf

    详谈java中File类getPath()、getAbsolutePath()、getCanonical的区别

    下面小编就为大家带来一篇详谈java中File类getPath()、getAbsolutePath()、getCanonical的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Java基础知识小结

    1.2 file的getPath getAbsolutePath和getCanonicalPath的不同 . . . . . . .5 1.3 int String 互转的多种方法 . . . . . . . . . . . . . . . . . . . . . 10 1.4 JAVA注释方法及格式 . . . . . . . . . . . . . . ....

    测试文件(java版)

    System.out.println("绝对路径: " + file.getAbsolutePath()); System.out.println("最后修改时间:" + file.lastModified()); System.out.println("文件大小:" + file.length()+ " 字节"); } }

    Web安全测试软件GetPath

    Web安全测试软件GetPath Web安全测试软件GetPath

    getPath.html

    getPath.html

    ueditor-1.4.3_utf8_修正版本(解决windows下在线图库及上传图问题)

    String path = file.getAbsolutePath(); String str = ""; if(System.getProperty("os.name").toLowerCase().startsWith("windows")){ str = path.replace(this.rootPath.replaceAll("\\/", "\\\\"), "\\"); }...

    java压缩文件源码--ZipUtils

    FileOutputStream fos = new FileOutputStream(getFileName(file.getPath())+File.separator+newDir(file, entry.getName())); dest = new BufferedOutputStream(fos, BUFFER); while((count = zis...

    getpath 抓路径

    getpath 抓路径getpath 抓路径getpath 抓路径

    1648238771053337_getpath.do

    1648238771053337_getpath.do

    getpath.do

    getpath.do

    getPath getPath

    根目录 从工程开始 例如:F:\\test\\mv\\smash F盘下有个文件夹test,有在第二级目录下有个工程叫 smash 那么该包的方法可以定位到 F:\\test\\mv\\smash\\ 获得工程所在的根目录(绝对路径)

    GetPath 1.23.031.rar

    GetPath 1.23.031.rar 老版本的 ,速度很快的说。。扫目录很好啊啊~~

    java 文件处理代码

    File file = new File("C:\\z"); File subFile[] = file.listFiles(); int fileNum = subFile.length; for (int i = 0; i ; i++) { Long tempLong = new Long(subFile[i].lastModified()); tm.put...

    IO输入输出留

    1 File f=new File("d:/abc") (d:\\abc) 2 File f=new File ("目录","文件名") 创建一个 File 封装对象 可以不存在 方法 -------------------------------------------- 文件、目录属性方法 文件、目录属性 ...

    Getpath L&T Company trbbadboy Made

    File style为要提取的文件类型,和Windows搜索文件一样,(例如要提取Txt格式的就设为 *.Txt) 文件类型设置好后点Percolate确认,就能过滤。 单击驱动器选项下的列表框中的文件进行提取,如果是全部提取则可用键盘中...

Global site tag (gtag.js) - Google Analytics