Java的getPath()、getAbsolutePath()、getCanonicalPath()

 
    getAbsolutePath():
 
    返回抽象路径名的绝对路径名字符串。
 
    public static void test1(){
        File file1 = new File(".\\test1.txt");
        System.out.println("-----默认相对路径:取得路径不同------");
        System.out.println(file1.getPath());
        System.out.println(file1.getAbsolutePath());

        File file2 = new File("D:\\workspace\\test\\test1.txt");
        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
 
----------------------------------------------------
 
    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
 

 

你可能感兴趣的:(Java的getPath()、getAbsolutePath()、getCanonicalPath())