File的getPath getAbsolutePath和getCanonicalPath的不同
来源:互联网 发布:mac应用程序卸载 编辑:程序博客网 时间:2024/05/18 09:18
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.txtD:\workspace\test\.\test1.txt-----默认绝对路径:取得路径相同------D:\workspace\test\test1.txtD:\workspace\test\test1.txt
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\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这个文件,直接执行这段代码,得到的结果是:
注意这里试大写的Text.txt
(2)在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");决定了结果.
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- JAVA中file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- java.io.File 的 getPath getAbsolutePath 和 getCanonicalPath 的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- File的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- 程序员必须知道的10大基础实用算法及其讲解
- JS一行代码去除字符串首尾空格
- 点评网络上流传的提高性能的Java编码方式
- 分享一个失真度较小的图片缩小方法
- 关于Spring中常被问到的一些问题
- File的getPath getAbsolutePath和getCanonicalPath的不同
- Linux下分析日志常用命令
- CMake 简明教程(2)---编译库文件
- Mysql大数据表删除重复数据
- 总结一下用caffe跑图片数据的研究流程
- ReentrantReadWriteLock优势及使用
- Mysql中Insert into xxx on duplicate key update和REPLACE INTO使用
- PHP抓取网页内容,获取链接绝对路径和图片绝对路径
- html容器中图片水平居中