File类的getPath()、getAbsolutePath()、getCanonicalPath()
来源:互联网 发布:windows 64位汇编语言 编辑:程序博客网 时间:2024/06/08 02:44
File类提供了getPath()、getAbsolutePath()、getCanonicalPath()三个方法来提供文件路径,本文将通过下面的实例演示它们的区别与联系:
public class FileTest { static void printPath(String path){ System.out.println("输入:" + path); File file = new File(path); printPath(file); } static void printPath(File file){ System.out.println(file.getPath()); System.out.println(file.getAbsolutePath()); try { System.out.println(file.getCanonicalPath()); } catch (IOException e) { System.err.println("Error occurs when invoking getCanonicalPath()"); } System.out.println(); } public static void main(String[] args) { printPath("C:\\Users\\qwer\\Desktop\\osu\\图解HTTP.pdf"); printPath("图解HTTP.pdf"); printPath(".\\图解HTTP.pdf"); printPath("..\\不存在的目录\\图解HTTP.pdf"); }}结果为:输入:C:\Users\qwer\Desktop\osu\图解HTTP.pdfC:\Users\qwer\Desktop\osu\图解HTTP.pdfC:\Users\qwer\Desktop\osu\图解HTTP.pdfC:\Users\qwer\Desktop\osu\图解HTTP.pdf输入:图解HTTP.pdf图解HTTP.pdfD:\eclipsepreferences\Tests\图解HTTP.pdfD:\eclipsepreferences\Tests\图解HTTP.pdf输入:.\图解HTTP.pdf.\图解HTTP.pdfD:\eclipsepreferences\Tests\.\图解HTTP.pdfD:\eclipsepreferences\Tests\图解HTTP.pdf输入:..\不存在的目录\图解HTTP.pdf..\不存在的目录\图解HTTP.pdfD:\eclipsepreferences\Tests\..\不存在的目录\图解HTTP.pdfD:\eclipsepreferences\不存在的目录\图解HTTP.pdf
用于测试的路径中,第一个是绝对路径,后三个都是相对路径。从输出结果中,可以得出几点结论:
(1)对于绝对路径,三种方法的输出结果相同。
(2)对于相对路径,getPath()不做任何处理,输出路径等于输入路径;getAbsolutePath()简单地将当前工作目录(即项目文件夹路径,本文中为D:\eclipsepreferences\Tests\)与输入的相对路径拼接在一起,包括.\与..\;getCanonicalPath()会在getAbsolutePath()的基础上解析出完整的路径。
(3)Java中,相对路径的根默认在项目文件夹路径。
(4)getCanonicalPath()返回的必定是绝对路径。
阅读全文
0 0
- File类的getPath()、getAbsolutePath()、getCanonicalPath()
- File:getPath()&getAbsolutePath()&getCanonicalPath()
- java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别
- file类中getPath()、getAbsolutePath()、getCanonicalPath()的区别
- java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别
- java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别
- java中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的不同
- JAVA中file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- file的getPath getAbsolutePath和getCanonicalPath的不同
- Python之tkinter模块实现登录功能
- 基于心跳的socket长连接
- Android 6.0 之后动态权限申请
- Linux的iptables防火墙配置详解
- 睡到了一点半
- File类的getPath()、getAbsolutePath()、getCanonicalPath()
- Android中ConvenientBanner 网络上的图片
- 关于字典dict中的迭代
- 【spark】spark-hive操作
- 在搜索引擎推广自己的网站(比如,百度)
- 2013-2014 ACM-ICPC Brazil Subregional Programming Contest
- 多线程爬坑记--基础篇(一)
- CentOS下mysql的安装
- 基于RPGMakerMV的JavaScript基础-6