How do I get a file's directory using the File object?
In either case, I'd expect file.getParent()
(or file.getParentFile()
) to give you what you want.
Additionally, if you want to find out whether the original File
does exist and is a directory, then exists()
and isDirectory()
are what you're after.
File.getParent() from Java Documentation
If you do something like this:
File file = new File("test.txt");
String parent = file.getParent();
parent
will be null.
So to get directory of this file you can do next:
parent = file.getAbsoluteFile().getParent();