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.
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.
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.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With