JAVA 使用File對象getCanonicalPath注意點

首先來介紹File對象中 getPath()方法,getAbsolutePath()方法及重點要說的getCanonicalPath()方法;

java.io.File 包含三種確定文件路徑的方法:

  1. getPath():此文件路徑方法將抽象路徑名作為String返回。如果字符串pathname用于創(chuàng)建File對象,則getPath()只返回pathname參數(shù),例如File file = new File(pathname) 構造參數(shù)pathname是怎么樣,getPath()就返回怎么的字符串。如果URI用作參數(shù),則它將刪除協(xié)議并返回文件名。

  2. getAbsolutePath():此文件路徑方法返回文件的絕對路徑。如果使用絕對路徑名創(chuàng)建File對象,則只返回路徑名。

    如果使用相對路徑創(chuàng)建文件對象,則以系統(tǒng)相關的方式解析絕對路徑名。在UNIX系統(tǒng)上,通過將相對路徑名解析為當前用戶目錄,使其成為絕對路徑名。

    在Microsoft Windows系統(tǒng)上,通過將路徑名解析為路徑名所指定的驅動器的當前目錄(如果有),使相對路徑名成為絕對路徑名; 如果沒有,則針對當前用戶目錄解析。

  3. getCanonicalPath():此路徑方法返回絕對唯一的標準規(guī)范路徑名。此方法首先將此路徑名轉換為絕對形式,就像調用getAbsolutePath方法一樣,然后以系統(tǒng)相關的方式將其映射到其唯一路徑上。
    也就是說如果路徑中包含“.”或“..”等當前路徑及上層路徑表示法,則會從路徑名中刪除“.”和“..”使用真實路徑代替。另外比較重點的是 它還會解析軟鏈接(在UNIX平臺上)以及將驅動器號(在Microsoft Windows平臺上),將它們轉換為標準實際路徑。

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created by martin on 2019/4/25.
 */
public class JavaFilePathTest {

    public static void main(String[] args) throws IOException, URISyntaxException {
        File file = new File("/Users/martin6699/test.txt");
        printPaths(file);

        // relative path
        file = new File("test.xsd");
        printPaths(file);
        // complex relative paths
        file = new File("/Users/martin6699/../martin6699/test.txt");
        printPaths(file);
        // URI paths
        file = new File(new URI("file:///Users/martin6699/test.txt"));
        printPaths(file);

        // symbolic links  軟鏈接 /Users/martin6699/logs ---> /tmp/Data/logs
        file = new File("/Users/martin6699/logs/test.txt");
        printPaths(file);

    }

    private static void printPaths(File file) throws IOException {
        System.out.println("Absolute Path: " + file.getAbsolutePath());
        System.out.println("Canonical Path: " + file.getCanonicalPath());
        System.out.println("Path: " + file.getPath());
        System.out.println("----------------------------------------------------------------");
    }

}

輸出

Absolute Path: /Users/martin6699/test.txt
Canonical Path: /Users/martin6699/test.txt
Path: /Users/martin6699/test.txt
----------------------------------------------------------------
Absolute Path: /Users/martin/Documents/backend/me/test.xsd
Canonical Path: /Users/martin/Documents/backend/me/test.xsd
Path: test.xsd
----------------------------------------------------------------
Absolute Path: /Users/martin6699/../martin6699/test.txt
Canonical Path: /Users/martin6699/test.txt
Path: /Users/martin6699/../martin6699/test.txt
----------------------------------------------------------------
Absolute Path: /Users/martin6699/test.txt
Canonical Path: /Users/martin6699/test.txt
Path: /Users/martin6699/test.txt
----------------------------------------------------------------
Absolute Path: /Users/martin6699/logs/test.txt
Canonical Path: /tmp/Data/logs/test.txt
Path: /Users/martin6699/logs/test.txt
----------------------------------------------------------------
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容