Java中的 File isAbsolute() 方法及示例
isAbsolute()方法是 File 类的一部分。该函数返回抽象路径名是否是绝对的。
例如:如果我们使用路径作为“program.txt”创建一个文件对象,它指向存在于保存可执行程序的同一目录中的文件(如果您使用的是 IDE,它将指向您所在的文件)已保存程序)。这里上面提到的文件的路径是“program.txt”,但是这个路径不是绝对的(即不完整的)。绝对路径是从根目录开始的完整路径。
函数签名:
public boolean isAbsolute()
函数语法:
file.isAbsolute()
参数:此函数不接受任何参数。
返回值:该函数返回布尔值,它告诉抽象路径名是否是绝对的。
下面的程序将说明 isAbsolute()函数的使用
示例1:给定一个文件的文件对象,我们要检查它是否是绝对的
// Java program to demonstrate the
// use of isAbsolute() function
import java.io.*;
public class solution {
public static void main(String args[])
{
// try-catch block to handle exceptions
try {
// Create a file object
File f = new File("c:\\users\\program.txt");
// Check if the given path
// is absolute or not
if (f.isAbsolute()) {
// Display that the path is absolute
// as the function returned true
System.out.println("The path is absolute");
}
else {
// Display that the path is not absolute
// as the function returned false
System.out.println("The path is not absolute");
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
输出:
The path is absolute
示例2:给定一个文件的文件对象,我们要检查它是否是绝对的
// Java program to demonstrate the
// use of isAbsolute() function
import java.io.*;
public class solution {
public static void main(String args[])
{
// try-catch block to handle exceptions
try {
// Create a file object
File f = new File("program.txt");
// Check if the given path
// is absolute or not
if (f.isAbsolute()) {
// Display that the path is absolute
// as the function returned true
System.out.println("The path is absolute");
}
else {
// Display that the path is not absolute
// as the function returned false
System.out.println("The path is not absolute");
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
输出:
The path is not absolute
这些程序可能无法在在线 IDE 中运行。请使用离线IDE并设置文件的父文件