sutoL sutoL - 3 months ago 15
Java Question

Moving to a directory one level down

Is it possible to move to a directory one level down in Java?

For example in command prompt:

C:\Users\foo\


I can use
cd..
to go to:

C:\Users\


Is it possible to do this in
Java
, because I'm getting a directory using System.getProperty("user.dir"); however that is not the directory I'd want to work at, but rather 1 level down the directory.

I have thought of using the Path class method;
subpath(i,j)
, but if the "user.dir" were to be changed to another directory, then the returned
subpath
would be different.

Answer

The File class can do this natively.

File upOne = new File(System.getProperty("user.dir")).getParentFile()

http://docs.oracle.com/javase/6/docs/api/java/io/File.html#getParentFile%28%29