例子
以下示例显示java.io.File.getAbsolutePath()方法的用法。
package com.jc2182;
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