1

I am trying to create a directory inside the folllowing path /var/www/downloads/ with this String name organization.id but I am getting a false as output.

    File filePath = new java.io.File("/var/www/downloads/" + organization.id).mkdir();
    String test = filePath.toString();  
    println("--> Path " + test);
2
  • java7fs.wikia.com/wiki/Why_File_sucks. Start using utils added in Java 7 which allows you to know what exactly caused the problem instead of returning boolean. Commented Aug 25, 2016 at 10:22
  • Also your example doesn't compile since mkdir() returns boolean but you are storing it in File. Another confusing part is: why do you have java.io.File in your code? You already have File filePath so it looks like you added java.io.File or java.io.* to your imports. Does File type of filePath not come from java.io package? Commented Aug 25, 2016 at 11:07

2 Answers 2

3

it's better to use java.nio.file.Paths and java.nio.file.Files:

Path path = Paths.get("/var/www/downloads/" + organization.id);
if (!Files.exists(path)) {    //    check if directory exists
    try {
        Files.createDirectories(path);
        System.out.println("Directory created SUCCESSFULLY.");
    } catch (IOException e) { //    failed to create
        System.out.println("Directory creation FAILED.");
        e.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

mkdir() returns boolean. So assign new java.io.File("/var/www/downloads/" + organization.id).mkdir(); to a boolean value and print it to check.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.