當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.io.File.getCanonicalPath()用法及代碼示例


描述

這個java.io.File.getCanonicalPath()方法返回此抽象路徑名的規範路徑名字符串。此方法從路徑名中刪除多餘的名稱,例如 "." 和 ".."

聲明

以下是聲明java.io.File.getCanonicalPath()方法 -

public String getCanonicalPath()

參數

NA

返回值

該方法返回規範路徑名字符串。

異常

  • IOException− 如果發生 I/O 錯誤。

  • SecurityException− 如果無法訪問係統屬性值。

示例

下麵的例子展示了 java.io.File.getCanonicalPath() 方法的用法。

package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      String path = "";
      boolean bool = false;
      
      try {
         // create new files
         f = new File("C:\\Program Files\\..\\test.txt");
         
         // create new canonical form file object
         path = f.getCanonicalPath();
         
         // if the file path exists
         bool = f.exists();
         
         // if file exists
         if(bool) {
         
            // prints
            System.out.print(path+" Exists? "+ bool);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

C:\test.txt Exists? true

相關用法


注:本文由純淨天空篩選整理自 Java.io.File.getCanonicalPath() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。