Java File getAbsoluteFile() method with examples

December 02, 2019 No comments Java IO File Examples getAbsoluteFile

1. Introduction

The getAbsoluteFile() method returns the absolute form of the given File.

2. Method signature

public File getAbsoluteFile()


  • method does not take any parameter


  • the absolute abstract pathname indicating the same file or directory as this abstract pathname


  • SecurityException - when we do not have access to the file or directory

3. Examples

3.1. The program that gets the absolute file from /tmp/frontbackend.txt



public class FrontBackend {

    public static void main(String args[]) {

        try {
            File file = new File("/tmp/frontbackend.txt");

            File absoluteFile = file.getAbsoluteFile();

            System.out.println(absoluteFile.getAbsolutePath()); // /tmp/frontbackend.txt

            System.out.println(absoluteFile.equals(file)); // true

        } catch (Exception e) {

The output:


4. Conclusion

In this article we presented how to use the File.getAbsoluteFile() method. The method can be helpful if you create a file using a relative path and want to retrieve information about the parent directory.

{{ message }}

{{ 'Comments are closed.' | trans }}