Java文件类boolean isAbsolute()方法(带示例)

文件类boolean isAbsolute() (File Class boolean isAbsolute())

  • This method is available in package java.io.File.isAbsolute().

    软件包java.io.File.isAbsolute()中提供了此方法。

  • This method is used to check whether the file or directories path is absolute or not (i.e. Absolute path is the complete path like Drive:\\Foldername\\Filename).

    此方法用于检查文件或目录路径是否是绝对路径(即,绝对路径是完整路径,例如Drive:\\ Foldername \\ Filename)。

  • The return type of this method is boolean i.e true or false If it returns true that's means given file or directory path is absolute else return false file or directory path is not an absolute (i.e. Not complete path).

    此方法的返回类型为boolean,即true或false。如果返回true,则意味着给定的文件或目录路径是绝对的,否则返回false文件或目录路径不是绝对的(即,不完整的路径)。

Syntax:

句法:

    boolean isAbsolute(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the File.

我们不会在File方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is boolean, it returns true if the given path of file or directories is absolute else return false.

此方法的返回类型为boolean ,如果给定的文件或目录路径为绝对路径,则返回true,否则返回false。

Java程序演示isAbsolute()方法的示例 (Java program to demonstrate example of isAbsolute() method)

import java.io.*;

public class ToCheckAbsolutePath {
    public static void main(String[] args) {
        try {
            File path1 = new File("C:\\Users\\computerclinic\\OneDrive\\Articles\\myjava.txt");
            File path2 = new File("myjava1.txt");

            // By using isAbsolute() method of File will check 
            // whether the specified file path is absolute or not 
            // and isAbsolute() method works with File class object 
            // because of its File method and it return Boolean return 
            // true if file path is absolute , false otherwise.

            // isAbsolute() returns true here because file path is absolute here 
            if (path1.isAbsolute())
                System.out.println("Given path" + " " + path1.getPath() + " is absolute");
            else
                System.out.println("Given path" + " " + path1.getPath() + " is not absolute");

            // isAbsolute() returns false here because file path is not absolute here 
            if (path2.isAbsolute())
                System.out.println("Given path" + " " + path2.getPath() + " is absolute");
            else
                System.out.println("Given path" + " " + path2.getPath() + " is not absolute");
        } catch (Exception e) {
            System.err.println("An error occurred");
            e.printStackTrace();
        }
    }
}

Output

输出量

E:\Programs>javac ToCheckAbsolutePath.java

E:\Programs>java ToCheckAbsolutePath
Given path C:\Users\computerclinic\OneDrive\Articles\myjava.txt is absolute
Given path myjava1.txt is not absolute


翻译自: https://www.includehelp.com/java/file-class-boolean-isabsolute-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值