简体   繁体   中英

folder not creating in linux through java program

I am trying to create folder and write images in it from war using following code:

// war directory : /opt/apache-tomcat/webapps/mj.war

String absoluteDiskPath = "tmp/mjpics/images/travel_schedule";
File file = new File(absoluteDiskPath);
if (!file.exists()) {
    if (file.mkdir()) {
        System.out.println("Directory is created!");
        try {
            writeText(textcontent, textFileName, eventDate, eventCat, absoluteDiskPath+"\\"+eventCat+"\\"+eventName);
            writeImage(imagecontent, imageFileName, eventDate, eventCat, absoluteDiskPath+"\\"+eventCat+"\\"+eventName);
            imagecontent.close();
            textcontent.close();
            UplodedData.flush();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    } else {
        System.out.println("Failed to create directory!");
        return false;
    }
}

Ouput: Failed to create directory.

Your absoluteDiskPath isn't absolute. Not sure if that's intentional, but you are missing a slash in front of it. Also, I am guessing, you want .mkdirs instead of .mkdir . The plural form creates all the folders in the path, the singular will only create the last one, and fail if the rest of the path does not exist.

Ie, if you are trying to create a folder "foo/bar/baz", .mkdir will fail unless you already have a folder " foo" in your current directory, containing a folder named "bar" inside of it.

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