File getAbsoluteFile()
描述 (Description)
java.io.File.getAbsoluteFile()方法返回此抽象路径名的绝对形式。
声明 (Declaration)
以下是java.io.File.getAbsoluteFile()方法的声明 -
public File getAbsoluteFile()
参数 (Parameters)
NA
返回值 (Return Value)
该方法返回由绝对抽象路径名定义的相同文件。
异常 (Exception)
SecurityException - 如果无法访问系统属性值。
例子 (Example)
以下示例显示了java.io.File.getAbsoluteFile()方法的用法。
package com.iowiki;
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("test.txt");
// create new file in the system
f.createNewFile();
// create new file object from the absolute path
f1 = f.getAbsoluteFile();
// 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();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
D:\EclipseAndroid\IO\BufferedInputStream\test.txt Exists? true