In Java 7 or higher, you can use Java NIO API's
Path.toAbsolutePath() method to get the absolute path of a file:
Path path = Paths.get("input.txt"); // get absolute path String filePath = path.toAbsolutePath().toString(); // print absolute path System.out.println(filePath);
Alternatively, you can also call
getAbsolutePath() on a
File object to get the file's complete path as shown below:
File file = new File("input.txt"); // get absolute path String filePath = file.getAbsolutePath(); // print absolute path System.out.println(filePath);
You may be interested in other Java I/O articles:
- Reading and Writing Files in Java
- How to Read and Write Text Files in Java
- How to Read and Write Binary Files in Java
- Reading and Writing Files using Java NIO API
- How to read a file line by line in Java
👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, ☕ Buy me a coffee (cost $5) .
Need help to launch a new product? I am available for contract work. Hire me to accomplish your business goals with engineering and design.