首先來(lái)介紹File對(duì)象中 getPath()方法,getAbsolutePath()方法及重點(diǎn)要說(shuō)的getCanonicalPath()方法囤官;
java.io.File
包含三種確定文件路徑的方法:
getPath()
:此文件路徑方法將抽象路徑名作為String返回。如果字符串pathname用于創(chuàng)建File對(duì)象帽馋,則getPath()只返回pathname參數(shù)片拍,例如File file = new File(pathname)
構(gòu)造參數(shù)pathname是怎么樣流译,getPath()就返回怎么的字符串。如果URI用作參數(shù)黔州,則它將刪除協(xié)議并返回文件名耍鬓。-
getAbsolutePath()
:此文件路徑方法返回文件的絕對(duì)路徑。如果使用絕對(duì)路徑名創(chuàng)建File對(duì)象流妻,則只返回路徑名牲蜀。如果使用相對(duì)路徑創(chuàng)建文件對(duì)象,則以系統(tǒng)相關(guān)的方式解析絕對(duì)路徑名绅这。在UNIX系統(tǒng)上各薇,通過(guò)將相對(duì)路徑名解析為當(dāng)前用戶目錄,使其成為絕對(duì)路徑名君躺。
在Microsoft Windows系統(tǒng)上峭判,通過(guò)將路徑名解析為路徑名所指定的驅(qū)動(dòng)器的當(dāng)前目錄(如果有),使相對(duì)路徑名成為絕對(duì)路徑名; 如果沒(méi)有棕叫,則針對(duì)當(dāng)前用戶目錄解析林螃。
getCanonicalPath()
:此路徑方法返回絕對(duì)唯一的標(biāo)準(zhǔn)規(guī)范路徑名。此方法首先將此路徑名轉(zhuǎn)換為絕對(duì)形式俺泣,就像調(diào)用getAbsolutePath方法一樣疗认,然后以系統(tǒng)相關(guān)的方式將其映射到其唯一路徑上。
也就是說(shuō)如果路徑中包含“.”或“..”等當(dāng)前路徑及上層路徑表示法伏钠,則會(huì)從路徑名中刪除“.”和“..”使用真實(shí)路徑代替横漏。另外比較重點(diǎn)的是 它還會(huì)解析軟鏈接(在UNIX平臺(tái)上)以及將驅(qū)動(dòng)器號(hào)(在Microsoft Windows平臺(tái)上),將它們轉(zhuǎn)換為標(biāo)準(zhǔn)實(shí)際路徑熟掂。
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
----------------------------------------------------------------