开发者

How to get absolute path of directory of a file?

开发者 https://www.devze.com 2023-03-08 07:19 出处:网络
How can I get an absolute path of a directory containing a file specified: // cu开发者_StackOverflow社区rrent dir is \"/home/me/dev\"

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

// cu开发者_StackOverflow社区rrent 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");
0

精彩评论

暂无评论...
验证码 换一张
取 消