File getAbsolutePath() method in Java with Examples
The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object.If the pathname of the file object is absolute then it simply returns the path of the current 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 getAbsolutePath() will return the absolute (complete) path from the root directories. If the file object is created with an absolute path then getPath() and getAbsolutePath() will give the same results.
Function Signature:
public String getAbsolutePath()
Function Syntax:
file.getAbsolutePath()
Parameters: This function does not accept any parameters.
Return value: This function returns a String value which is the absolute Path of the given File object.
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: A file named “program.txt” is in the present working directory.
// Java program to demonstrate the // use of getAbsolutePath() 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"); // Get the absolute path of file f String absolute = f.getAbsolutePath(); // Display the file path of the file object // and also the file path of absolute file System.out.println("Original path: " + f.getPath()); System.out.println("Absolute path: " + absolute); } catch (Exception e) { System.err.println(e.getMessage()); } } } |
Output:
Original Path: program.txt Absolute Path: C:\Users\pc\eclipse-workspace1\arnab\program.txt

Example 2: A directory named “program” is in the present working directory.
// Java program to demonstrate the // use of getAbsolutePath() function import java.io.*; public class solution { public static void main(String try-catch { // try catch block to handle exceptions try { // Create a file object File f = new File("program"); // Get the absolute path of file f String absolute = f.getAbsolutePath(); // Display the file path of the file object // and also the file path of absolute file System.out.println("Original path: " + f.getPath()); System.out.println("Absolute path: " + absolute); } catch (Exception e) { System.err.println(e.getMessage()); } } } |
Output:
Original Path: program Absolute Path: C:\Users\pc\eclipse-workspace1\arnab\program

Example 3: A file named “f:\program.txt” in the “f:\” directory.
// Java program to demonstrate the // use of getAbsolutePath() 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("f:\\program.txt"); // get the absolute path // of file f String absolute = f.getAbsolutePath(); // display the file path of the file object // and also the file path of absolute file System.out.println("Original path: " + f.getPath()); System.out.println("Absolute path: " + absolute); } catch (Exception e) { System.err.println(e.getMessage()); } } } |
Output:
Original file path: f:\program.txt Absolute file path: f:\program.txt

The programs might not run in an online IDE . please use an offline IDE and set the path of the file
Recommended Posts:
- File getAbsoluteFile() method in Java with Examples
- File exists() method in Java with examples
- File length() method in Java with Examples
- File delete() method in Java with Examples
- File createTempFile() method in Java with Examples
- File getFreeSpace() method in Java with examples
- File getTotalSpace() method in Java with examples
- File getUsableSpace() method in java with examples
- File isFile() method in Java with Examples
- File createNewFile() method in Java with Examples
- File getCanonicalPath() method in Java with Examples
- File canExecute() method in Java with Examples
- File isDirectory() method in Java with Examples
- File canRead() method in Java with Examples
- File canWrite() method in Java with examples
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.



