File isAbsolute() method in Java with Examples

The isAbsolute() method is a part of File class. The function returns whether the abstract pathname is absolute or not.

For Example: if we create a file object using the path as “program.txt”, it points to the file present in the same directory where the executable program is kept (if you are using a IDE it will point to the file where you have saved the program ). Here the path of the file mentioned above is “program.txt” but this path is not absolute (i.e. not complete) .Absolute path is the complete path from the root directory.

Function Signature:

public boolean isAbsolute()

Function Syntax:

file.isAbsolute()

Parameters: This function does not accept any parameters.

Return value: The function returns boolean value which tells whether the abstract pathname is absolute or not.

Below programs will illustrate the use of the isAbsolute() function

Example 1: We are given a file object of a file, we have to check whether it is absolute or not

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


Output:

The path is absolute

Example 2: We are given a file object of a file, we have to check whether it is absolute or not

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


Output:

The path is not absolute

The programs might not run in an online IDE. please use an offline IDE and set the Parent file of the file



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.