Open In App

File isAbsolute() method in Java with Examples

Last Updated : 30 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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




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


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




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


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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads