Itzhak
Itzhak

Reputation: 47

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\Project"

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?

Upvotes: 2

Views: 5534

Answers (2)

sauumum
sauumum

Reputation: 1788

Step 1 : Create a file with path as

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

Step 2 : Check file.isDirectory(); Tests whether the file denoted by this abstract pathname is a directory.

  1. File parentFolder = file.getParentFile(); Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory.

  2. Make new directory in parent folder.

Upvotes: 0

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

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.

Upvotes: 1

Related Questions