简体   繁体   中英

create a directory in existing path in java?

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);

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();
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM