Java.io.File.getPath() 方法
描述
java.io.File.getPath() 方法将抽象路径名转换为路径名字符串。 为了分隔名称序列中的名称,生成的字符串使用默认的名称分隔符。
声明
以下是 java.io.File.getPath() 方法的声明 −
public String getPath()
参数
NA
返回值
该方法返回此抽象路径名的路径名字符串形式。
异常
NA
示例
下面的例子展示了 java.io.File.getPath() 方法的使用。
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
String v;
boolean bool = false;
try {
// create new file
f = new File("test.txt");
// pathname string from abstract pathname
v = f.getPath();
// true if the file path exists
bool = f.exists();
// if file exists
if(bool) {
// prints
System.out.print("pathname: "+v);
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
pathname: test.txt

