file.getPath() VS getAbsolutePath() VS getCanonicalPath()

来源:互联网 发布:瀚资软件咨询有限公司 编辑:程序博客网 时间:2024/06/08 07:08

区别

  1. getPath()返回的是构造方法里的路径,不做任何处理;
  2. getAbsolutePath()返回的是 user.dir+getPath(),也就是执行路径/用户工作目录加上构造方法中的路径;
  3. getCanonicalPath()返回的是将符号完全解析的路径,也就是全路径;

代码示例

import java.io.File;public class FilePathTest {    public static void main(String[] args) throws Exception {        System.out.println("工作目录:" + System.getProperty("user.dir"));        File f = new File("../tt.txt");        System.out.println("f.getPath(): " + f.getPath());        System.out.println("f.getAbsolutePath(): " + f.getAbsolutePath());        System.out.println("f.getCanonicalPath(): " + f.getCanonicalPath());    }}

输出结果:

工作目录:D:\projects\nettyf.getPath(): ..\tt.txtf.getAbsolutePath(): D:\projects\netty\..\tt.txtf.getCanonicalPath(): D:\projects\tt.txt
阅读全文
0 0
原创粉丝点击