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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.