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.

Taufik Pirjade
  • 380
  • 6
  • 26
  • 1
    Add an exception block to check for the error message why is couldn't create the dir. Mostly a permissions issue – Harinder Jan 02 '16 at 14:08
  • 2
    Possible duplicate of [File.mkdir or mkdirs return false - Reason?](http://stackoverflow.com/questions/12202766/file-mkdir-or-mkdirs-return-false-reason) – TZHX Jan 02 '16 at 14:08
  • Use `mkdirs()` instead, if parent dirs do not exist – John_West Jan 02 '16 at 14:12

1 Answers1

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.

Dima
  • 39,570
  • 6
  • 44
  • 70