Java-io-file-getcanonicalfile
提供:Dev Guides
Java.io.File.getCanonicalFile()メソッド
説明
- java.io.File.getCanonicalFile()*メソッドは、この抽象パス名の標準形式を返します。
宣言
以下は* java.io.File.getCanonicalFile()*メソッドの宣言です-
public File getCanonicalFile()
パラメーター
NA
戻り値
このメソッドは、正規のパス名文字列で表される同じファイルまたはディレクトリを返します。
例外
- IOException -I/Oエラーが発生した場合
- SecurityException -システムプロパティ値にアクセスできない場合。
例
次の例は、java.io.File.getCanonicalFile()メソッドの使用法を示しています。
package com.finddevguides;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
File f1 = null;
String path = "";
boolean bool = false;
try {
//create new files
f = new File("C:\\Program Files\\..\\test.txt");
//create new canonical form file object
f1 = f.getCanonicalFile();
//returns true if the file exists
bool = f1.exists();
//returns absolute pathname
path = f1.getAbsolutePath();
//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