Path toAbsolutePath() method in Java with Examples
The Java Path interface was added to Java NIO in Java 7. toAbsolutePath() method of java.nio.file.Path used to return a Path object representing the absolute path of this path object. If the path is already absolute then the method returns path else this method resolves this path in an implementation-dependent manner by resolving the path against a file system default directory. Depending on the implementation, this method may throw an I/O error if the file system is not accessible.
Syntax:
int toAbsolutePath()
Parameters: This method accepts nothing.
Return value: This method returns a Path object representing the absolute path.
Exception: This method throws following Exceptions:
- IOError – if an I/O error occurs
- SecurityException – In the case of the default provider, a security manager is installed, and this path is not absolute, then the security manager’s
- checkPropertyAccess method is invoked to check access to the system property user.dir
Below programs illustrate toAbsolutePath() method:
Program 1:
// Java program to demonstrate // java.nio.file.Path.toAbsolute() method import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static void main(String[] args) throws IOException { // create object of Path Path path = Paths.get("\\temp\\Spring"); // call toAbsolutePath() to get // absolute path Path isAbsolute = path.toAbsolutePath(); // print absolute path System.out.println("Absolute Path: " + isAbsolute); } } |
Program 2:
// Java program to demonstrate // java.nio.file.Path.toAbsolutePath() method import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static void main(String[] args) throws IOException { // create object of Path Path path = Paths.get("Program Files\\Java"); // call toAbsolutePath() to get // absolute path Path isAbsolute = path.toAbsolutePath(); // print absolute path System.out.println("Absolute Path: " + isAbsolute); } } |
References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#toAbsolutePath()
Recommended Posts:
- Path hashCode() method in Java with Examples
- Path resolve() method in Java with Examples
- Path getParent() method in Java with Examples
- Path compareTo() method in Java with Examples
- Path getNameCount() method in Java with Examples
- Path getFileName() method in Java with Examples
- Path isAbsolute() method in Java with Examples
- Path toFile() method in Java with Examples
- Path toUri() method in Java with Examples
- Path getFileSystem() method in Java with Examples
- Path getName(int) method in Java with Examples
- Path toString() method in Java with Examples
- Path getRoot() method in Java with Examples
- Path equals() method in Java with Examples
- Path relativize() 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.





