Java-io-file-getcanonicalpath
提供:Dev Guides
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.finddevguides;
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