# How to get the absolute path of a file in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 12, 2019. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-get-file-absolute-path

> A quick tutorial to learn how to get the complete path of a file in Java.

In Java 7 or higher, you can use Java NIO API's `Path.toAbsolutePath()` method to get the absolute path of a file:

```java
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:

```java
File file = new File("input.txt");

// get absolute path
String filePath = file.getAbsolutePath();

// print absolute path
System.out.println(filePath);
```

## Further Reading

You may be interested in other Java I/O articles:

* [Reading and Writing Files in Java](https://attacomsian.com/blog/java-read-write-files)
* [How to Read and Write Text Files in Java](https://attacomsian.com/blog/java-read-write-text-files)
* [How to Read and Write Binary Files in Java](https://attacomsian.com/blog/java-read-write-binary-files)
* [Reading and Writing Files using Java NIO API](https://attacomsian.com/blog/java-read-write-files-nio-api)
* [How to read a file line by line in Java](https://attacomsian.com/blog/how-to-read-a-file-line-by-line-in-java)
