This is a very similar question to this one: How to get just the parent directory name of a specific file
But there he wanted to get just the closest Parent directory, what I want is to be able to select other "Parents" such as:
bbb or ccc (considering the same example on the mentioned question)
File file = new File("C:/aaa/bbb/ccc/ddd/test.java");
I tried file.getParent().getParent();
which didn't work.
Note: if possible I don't want to use regex on the path.
getParent()
returns a String
- you can't call getParent()
on a String
.
Instead, you want getParentFile()
:
File grandparent = file.getParentFile().getParentFile();
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