Java.io.File.getAbsolutePath() 方法
描述
java.io.File.getAbsolutePath() 方法返回此抽象路径名的绝对路径名字符串。
声明
以下是 java.io.File.getAbsolutePath() 方法的声明 −
public String getAbsolutePath()
参数
NA
返回值
该方法返回表示同一文件或目录的绝对路径名。
异常
SecurityException − 如果无法访问系统属性值。
示例
下面的例子展示了 java.io.File.getAbsolutePath() 方法的使用。
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("test.txt");
// returns true if the file exists
bool = f.exists();
// if file exists
if(bool) {
// get absolute path
path = f.getAbsolutePath();
// prints
System.out.print("Absolute Pathname "+ path);
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Absolute Path D:\EclipseAndroid\IO\BufferedInputStream\test.txt

