I have a String file path
String filePath = "/tmp/test/save/data/java/"
and would like to remove the last 2 directories and last /
so that,
String filePath = "/tmp/test/save"
I don't want to create a substring removing the last 10 characters, as the directories might change in length.
I was thinking about doing a combination of split
(setting a limit and removing the final index), then join
to recreate the string with /
seperators, but wondering if there's a cleaner way ?
You could use java.nio.file.Path#getParent
.
String result = Paths.get("/tmp/test/save/data/java/").getParent()
.getParent().toString();