Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get absolute path of directory of a file?

Tags:

java

file-io

How can I get an absolute path of a directory containing a file specified:

// current dir is "/home/me/dev"
File file = new File("./target/test.txt");
assert absolute(file).equals("/home/me/dev/target");

It's Java 6.

like image 333
yegor256 Avatar asked May 24 '11 19:05

yegor256


People also ask

How do I find the absolute path of a file?

You can determine the absolute path of any file in Windows by right-clicking a file and then clicking Properties. In the file properties first look at the "Location:" which is the path to the file.

What is the absolute path of the directory?

An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.

How do I find absolute directory in Linux?

You can get absolute path or full path of a file in Linux using readlink command with -f option. It is also possible to provide directory as the argument not just files.


1 Answers

You mean the methods in the documentation?

File file = new File("./target/test.txt");
String dirPath = file.getAbsoluteFile().getParentFile().getAbsolutePath()
assert dirPath.equals("/home/me/dev/target");
like image 62
skaffman Avatar answered Sep 28 '22 03:09

skaffman