Bo0ked61
Bo0ked61

Reputation: 67

How to Set a Path to Create Directory

This question header isn't the best, but I am trying to create a directory from the person's computer in java, then when a button is clicked, to navigate to that created directory and create a file. I just need to get the path then create the directory. Here is a sample:

File theDir = new File("Users/" + System.getProperty("user.name") + "/InfoSaveFiles");

if (!theDir.exists()) {
    if (theDir.mkdirs()) {

    }
}

The only problem is that when I run this, (from a compiled jar file or from my IDE), it just creates a directory Users/(name)/InfoSaveFiles instead of navigating to that particular path and creating the directory InfoSaveFiles as I would like.

How can I make it go to the specified path, then create the directory?

Upvotes: 1

Views: 162

Answers (1)

devReddit
devReddit

Reputation: 2937

If you want to are using that file path, it will look for Users/(name)/InfoSaveFiles in the present working directory, in case of IDE, the root of the project file. So, if you add a slash / before it, like '/Users/(name)/InfoSaveFiles`, then it will look for the hierarchy from the root of the current heirarchy. In case of windows, from the drive you're running the program.

For example, if you set the file path with slash before it and you're running the program in the E:// drive, then the program will look for Users/(name)/InfoSaveFiles in that drive, and will create the hierarchy if not exists.

So, if you want to do that in specific drive, you need to mention that too at the front of that file path.

For Linux, starting with / indicates the root directory. So you can achieve it in linux giving the current file path you've mentioned with an added / in the front. It will look for that path from the root directory then.

Hope that answers your question.

Upvotes: 1

Related Questions