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()

Parameters:

  • method does not take any parameter

Returns

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

Throws

  • 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

package com.frontbackend.java.io.examples;

import java.io.File;

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) {
            e.printStackTrace();
        }
    }
}

The output:

/tmp/frontbackend.txt
true

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 }}