Java get parent of parent

directory, file, java

Solution

`getParent()` returns a `String` - you can't call `getParent()` on a `String`.

Instead, you want `getParentFile()`:

File grandparent = file.getParentFile().getParentFile();

Problem

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.

Original source

Related problems