It should be rather easy, but I could not find a clean way to do so. I'm trying to get the parent full path of parent directory in a path. Consider the following path: /bin/src/config/file
, I would like to get /bin/src/config
in Java. So I get a string and need to get the full path (absolute path, not just the name and not relative) of the parent directory. What is the cleanest way to do so?
Asked
Active
Viewed 6,050 times
3

TTaJTa4
- 810
- 1
- 8
- 22
-
1First result from google for `java parent directory path` query gives [How to get just the parent directory name of a specific file](https://stackoverflow.com/q/8197049). Isn't that what you wanted? – Pshemo Dec 03 '18 at 13:19
-
maybe the old [java.io.File](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/File.html) or the not so old [java.nio.file.Path](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Path.html) classes (both part of java.base) – user85421 Dec 03 '18 at 13:22
1 Answers
6
You can use this, which will print the folder your file is in where fileName
is your filename:
Path f = Paths.get(fileName);
System.out.println(f.getParent());
Eg for String fileName = "C:\\Users\\Me\\Documents\\video.html"
the output is C:\Users\Me\Documents
.

Mark
- 5,089
- 2
- 20
- 31