Getting the full path of parent directory [java]

时间:2018-12-03 13:17:38

标签: java

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?

1 个答案:

答案 0 :(得分:0)

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.