File getAbsoluteFile() method in Java with Examples

The getAbsoluteFile() method is a part of File class. This function returns the absolute File object of the given abstract pathname. The absolute file or directory points to the same file or directory as the given File object.

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 an 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). The function getAbsoluteFile() will return a file whose path will be an absolute (complete) path from the root directories. If the file object is created with an absolute path then getAbsoluteFile() will return a file similar to the current file.

Function Signature:



public File getAbsoluteFile()

Function Syntax:

file.getAbsoluteFile()

Parameters: This function does not accept any parameters.

Return value: The function returns the absolute file object denoting the same file or directory as the abstract pathname.

Exception: This method throws Security Exception if the required property value cannot be accessed.

Below programs will illustrate the use of getAbsolutePath() method:

Example 1: We have a file named “program.txt” in the present working directory.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// use of getAbsoluteFile() 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");
  
            // create a file with the absolute path
            // of file f
            File absolute = f.getAbsoluteFile();
  
            // display the file path of the file object
            // and also the file path of absolute file
            System.out.println("Original file path : "
                               + f.getPath());
            System.out.println("Absolute file path : "
                               + absolute.getPath());
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

chevron_right


Output:

Original file path : program.txt
Absolute file path : C:\Users\pc\eclipse-workspace1\arnab\program.txt

Example 2: We have a directory named “program” in the present working directory.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// use of getAbsoluteFile() 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");
  
            // create a file with the absolute path
            // of file f
            File absolute = f.getAbsoluteFile();
  
            // display the file path of the file object
            // and also the file path of absolute file
            System.out.println("Original file path : "
                               + f.getPath());
            System.out.println("Absolute file path : "
                               + absolute.getPath());
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

chevron_right


Output:

Original file path : program
Absolute file path : C:\Users\pc\eclipse-workspace1\arnab\program

The programs might not run in an online IDE. please use an offline IDE and set the path 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.