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
// 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
Recommended Posts:
- Path isAbsolute() method in Java with Examples
- File isHidden() method in Java with Examples
- File lastModified() method in Java with Examples
- File createNewFile() method in Java with Examples
- File createTempFile() method in Java with Examples
- File setExecutable() method in Java with Examples
- File getParentFile() method in Java with Examples
- File canExecute() method in Java with Examples
- File getPath() method in Java with Examples
- File length() method in Java with Examples
- File getCanonicalPath() method in Java with Examples
- File exists() method in Java with examples
- File isFile() method in Java with Examples
- File getAbsoluteFile() method in Java with Examples
- File listFiles() method in Java with Examples
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.



