Java NIO - 路徑



顧名思義,Path 是檔案系統中檔案或目錄等實體的特定位置,以便人們可以搜尋並在該特定位置訪問它。

從技術上講,在 Java 中,Path 是一個介面,它在 Java 7 版本期間引入到 Java NIO 檔案包中,並且是特定檔案系統中位置的表示。由於 Path 介面位於 Java NIO 包中,因此它的限定名稱為 java.nio.file.Path。

通常,實體的路徑可以分為兩種型別:一種是絕對路徑,另一種是相對路徑。顧名思義,絕對路徑是從根到實體所在位置的地址,而相對路徑是相對於其他路徑的地址。Path 在其定義中使用分隔符,Windows 使用“\”,Unix 作業系統使用“/”。

為了獲取 Path 的例項,我們可以使用 java.nio.file.Paths 類的靜態方法 **get()**。此方法將路徑字串或一系列字串(連線後形成路徑字串)轉換為 Path 例項。如果傳遞的引數包含非法字元,此方法還會丟擲執行時 InvalidPathException。

如上所述,透過傳遞根元素和定位檔案所需的完整目錄列表來檢索絕對路徑。而相對路徑可以透過將基路徑與相對路徑組合來檢索。以下示例將說明兩種路徑的檢索。

示例

package com.java.nio;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.file.FileSystem;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathDemo {
   public static void main(String[] args) throws IOException {
      Path relative = Paths.get("file2.txt");
      System.out.println("Relative path: " + relative);
      Path absolute = relative.toAbsolutePath();
      System.out.println("Absolute path: " + absolute);
   }
}

到目前為止,我們知道什麼是 Path 介面,為什麼需要它以及如何訪問它。現在我們將瞭解 Path 介面提供的重要方法。

Path 介面的重要方法

  • **getFileName()** − 返回建立此物件的 檔案系統。

  • **getName()** − 將此路徑的名稱元素作為 Path 物件返回。

  • **getNameCount()** − 返回路徑中名稱元素的數量。

  • **subpath()** − 返回作為此路徑的名稱元素子序列的相對 Path。

  • **getParent()** − 返回父路徑,如果此路徑沒有父路徑,則返回 null。

  • **getRoot()** − 將此路徑的根元件作為 Path 物件返回,如果此路徑沒有根元件,則返回 null。

  • **toAbsolutePath()** − 返回表示此路徑的絕對路徑的 Path 物件。

  • **toRealPath()** − 返回現有檔案的真實路徑。

  • **toFile()** − 返回表示此路徑的 File 物件。

  • **normalize()** − 返回此路徑,其中已消除冗餘名稱元素。

  • **compareTo(Path other)** − 按字典順序比較兩個抽象路徑。如果引數等於此路徑,此方法返回零;如果此路徑按字典順序小於引數,則返回小於零的值;如果此路徑按字典順序大於引數,則返回大於零的值。

  • **endsWith(Path other)** − 測試此路徑是否以給定路徑結尾。如果給定路徑有 N 個元素且沒有根元件,並且此路徑有 N 個或更多元素,則如果每個路徑的最後 N 個元素(從最遠離根的元素開始)相等,則此路徑以給定路徑結尾。

  • **endsWith(String other)** − 測試此路徑是否以 Path 結尾,該 Path 是透過以 endsWith(Path) 方法指定的方式轉換給定路徑字串構造的。

示例

以下示例說明了上面提到的 Path 介面的不同方法 −

package com.java.nio;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.file.FileSystem;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathDemo {
   public static void main(String[] args) throws IOException {
      Path path = Paths.get("D:/workspace/ContentW/Saurav_CV.docx");
      FileSystem fs =  path.getFileSystem();
      System.out.println(fs.toString());
      System.out.println(path.isAbsolute());
      System.out.println(path.getFileName());
      System.out.println(path.toAbsolutePath().toString());
      System.out.println(path.getRoot());
      System.out.println(path.getParent());
      System.out.println(path.getNameCount());
      System.out.println(path.getName(0));
      System.out.println(path.subpath(0, 2));
      System.out.println(path.toString());
      System.out.println(path.getNameCount());
      Path realPath = path.toRealPath(LinkOption.NOFOLLOW_LINKS);
      System.out.println(realPath.toString());
      String originalPath = "d:\\data\\projects\\a-project\\..\\another-project";
      Path path1 = Paths.get(originalPath);
      Path path2 = path1.normalize();
      System.out.println("path2 = " + path2);
   }
}
廣告