简体   繁体   中英

How to get absolute path of directory of a file?

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 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");
assert file.getParentFile().getAbsolutePath().equals("/home/me/dev/target");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM