Java.io.File.isAbsolute() 方法
描述
java.io.File.isAbsolute() 检查这个抽象路径名是否是绝对的。
声明
以下是 java.io.File.isAbsolute() 方法的声明 −
public boolean isAbsolute()
参数
NA
返回值
如果此抽象路径名是绝对路径,则该方法返回 true,否则该方法返回 false。
异常
NA
示例
下面的例子展示了 java.io.File.isAbsolute() 方法的使用。
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 file
f = new File("C:\\test.txt");
// true if the file path is absolute, else false
bool = f.isAbsolute();
// get the path
path = f.getPath();
// prints
System.out.println(path+" is absolute? "+ bool);
// create new file
f = new File("test.txt");
// true if the file path is absolute, else false
bool = f.isAbsolute();
// get the path
path = f.getPath();
// prints
System.out.print(path+" is absolute? "+bool);
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
C:\test.txt is absolute? false test.txt is absolute? false

