Home » Java programming language

Java File Class String getAbsolutePath() method with Example

Java File Class String getAbsolutePath() method: Here, we are going to learn about the String getAbsolutePath() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 11, 2019

File Class String getAbsolutePath()

  • This method is available in package java.io.File.getAbsolutePath().
  • This method is used to return the absolute path of the file object (i.e absolute path is the complete path like this c:\\ Articles\\myjava.txt) if filepath is absolute then it retrieve the complete path of the file object.
  • The return type of this method is String so it returns the absolute path from the root in a string form.
  • In this method, if we don’t give an absolute path in the file object then also it will return the absolute path of file object where your file exists.
  • This method may raise an exception( i.e. Security Exception) if the desired value cannot be accessed.

Syntax:

    String getAbsolutePath(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is String so it returns the complete path of the file object as a String.

Java program to demonstrate example of getAbsolutePath() method

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise an 
// exception when working with files
import java.lang.Exception;

public class GetAbsolutePath {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes to 
            // escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
            File file2 = new File("java.txt");

            // By using getAbsolutePath() return the complete 
            // path(whatever you have given in the file object) of the 
            // file object because in the file1 object we already given 
            // absolute path 
            // [C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt]
            String abs_path1 = file1.getAbsolutePath();

            // Display absolute path of the file object if given path is absolute.
            System.out.println("The path of the file 1 if given path is absolute :" + " " + abs_path1);

            // By using getAbsolutePath() return the complete path of the File 
            // object even we have not given full path or absolute path is 
            // not given [i.e.(java.txt) so it return the whole path 
            // with filename where file exists ]

            String abs_path2 = file2.getAbsolutePath();

            // Display absolute path of the file object if given path is not absolute.
            System.out.println("The path of the file2 if given path is not absolute :" + " " + abs_path2);

        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

D:\Programs>javac GetAbsolutePath.java

D:\Programs>java GetAbsolutePath
The path of the file 1 if given path is absolute : C:\Users\computer clinic\OneDrive\myjava.txt
The path of the file2 if given path is not absolute : D:\Programs\java.txt



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.