0

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.

3
  • 1
    Add an exception block to check for the error message why is couldn't create the dir. Mostly a permissions issue Commented Jan 2, 2016 at 14:08
  • 2
    Possible duplicate of File.mkdir or mkdirs return false - Reason? Commented Jan 2, 2016 at 14:08
  • Use mkdirs() instead, if parent dirs do not exist Commented Jan 2, 2016 at 14:12

1 Answer 1

3

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.

I.e., 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.

Sign up to request clarification or add additional context in comments.

5 Comments

parent dirs are existing, i want to create travel_schedule only
Then I guess, it's the missing slash problem. You are trying to create this patn in your current directory, which is almost always wrong.
their are two different directories /tmp/mjpics and war is in another directories
This has nothing to do with where war is.
Worked for me String absoluteDiskPath = "/tmp/mjpics/images/travel_schedule"

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.