简体   繁体   English

无法通过Java程序在Linux中创建文件夹

[英]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. Ouput:无法创建目录。

Your absoluteDiskPath isn't absolute. 您的absoluteDiskPath不是绝对的。 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 . 另外,我猜,您想要.mkdirs而不是.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. 即,如果您试图创建一个文件夹“ foo / bar / baz”,。 .mkdir将失败,除非您在当前目录中已有一个名为“ foo”的文件夹,并且其中包含名为“ bar”的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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