Home » Java programming language

Java File Class boolean isAbsolute() method with Example

Java File Class boolean isAbsolute() method: Here, we are going to learn about the boolean isAbsolute() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 15, 2019

File Class boolean isAbsolute()

  • This method is available in package 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).
  • 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).

Syntax:

    boolean isAbsolute(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the 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.

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


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.