12

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.

2 Answers 2

30

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"); 
Sign up to request clarification or add additional context in comments.

Comments

2
assert file.getParentFile().getAbsolutePath().equals("/home/me/dev/target"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.