String getCanonicalPath()
描述 (Description)
java.io.File.getCanonicalPath()方法返回此抽象路径名的规范路径名字符串。 此方法删除多余的名称,如“。” 和路径名中的“..”
声明 (Declaration)
以下是java.io.File.getCanonicalPath()方法的声明 -
public String getCanonicalPath()
参数 (Parameters)
NA
返回值 (Return Value)
该方法返回规范路径名字符串。
异常 (Exception)
IOException - 如果发生I/O错误。
SecurityException - 如果无法访问系统属性值。
例子 (Example)
以下示例显示了java.io.File.getCanonicalPath()方法的用法。
package com.iowiki;
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