简体   繁体   中英

Get parent directory's parent directory name in java

I have current working file's directory path I:\\apache-tomcat-7.0.40\\webapps\\ExecutableFileProcess\\WEB-INF\\classes\\PackageName\\

I want path like I:\\apache-tomcat-7.0.40\\webapps\\ExecutableFileProcess\\ . How can I do this in java code?

You better get your App-Path from the ServletContext in Servlet-Containern like Tomcat.

Use

servletContext.getRealPath(".");
// will return 'I:\apache-tomcat-7.0.40\webapps\ExecutableFileProcess\' on win.
// will return '/usr/local/tomcat/webapps/ExecutableFileProcess' on unix.

Have a look at

http://developer.android.com/reference/java/io/File.html#getParentFile()

Applying this twice will probably get you the right directory.

But note that you program might or might not be allowed to go there, based on the settings in the file system and the context your code runs in.

Try this:

Path aPath = Paths.get("I:\\apache-tomcat-7.0.40\\webapps\\ExecutableFileProcess\\WEB-INF\\classes\PackageName\\");
Path parentsParentpath = aPath.getParent().getParent();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM