Reputation: 47
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
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.
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.
Make new directory in parent folder.
Upvotes: 0
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