Itzhak Itzhak - 5 months ago 14
Java Question

Get path to directory, that next to project directory - JAVA

I need to create directory for image storing next to project root directory.

I know that project root directory path I can get this way:

String PROJECT_ROOT_PATH = System.getProperty("user.dir");


If I do this way:

String PATH_TO_PACKAGE = System.getProperty("user.dir") + "\\imagesForTesting\\";


-- it will create directory in project root directory.

But I want to create directory not in, but next to project root directory.


For example, I have PROJECT_ROOT_PATH = "D:\Work\SeleniumProject"


And I need that new directory for images will be in:


PATH_TO_PACKAGE =
"D:\Work\imagesForTesting";


In windows console it is pretty simple: to get level up you just have to use "cd .."

Is it possible to do such thing in Java?

Answer

Yes Java supports relative path such that you can use .. in your path to access to the parent directory, in other words this can be done:

String PATH_TO_PACKAGE = System.getProperty("user.dir") + "\\..\\imagesForTesting\\";

NB: You can use / instead of \\ to build your path, it will still work on windows OS.

Comments