简体   繁体   English

Java IO-文件夹创建错误

[英]Java IO - Folder Creation Error

I am not able to create a folder using following code. 我无法使用以下代码创建文件夹。

import java.io.File;

public class Fileupload
{
public static void main(String[] args) 
    {
        File f = new File("C:\\BOS\\BOS-5.8-Tomcat-6.0.35\\webapps\\bonita\\ECR-DZ-00013\\Ranjeet\\");
        if (f.exists())
        {
            System.out.println("Already Present");
        }
        else
        {
            f.mkdir();
            System.out.println("Created");
        }
    }
 }

If I remove my name "Ranjeet" from the path then it gets created, otherwise folder is not created by the same code. 如果我从路径中删除我的名字“ Ranjeet”,则会创建它,否则,文件夹不会由相同的代码创建。

We don't know enough to be sure that this is the answer, but... 我们还不足以确保这是答案,但是...

mkdir relies on the parent directory already existing. mkdir依赖于已经存在的父目录。 So it won't be able to create the Ranjeet directory unless ECR-DZ-00013 already exists. 因此,除非ECR-DZ-00013已经存在,否则它将无法创建Ranjeet目录。

However, you can use mkdirs instead which creates all the intermediate directories as required: 但是,可以改用mkdirs来创建所需的所有中间目录:

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. 创建以此抽象路径名命名的目录,包括任何必需但不存在的父目录。 Note that if this operation fails it may have succeeded in creating some of the necessary parent directories. 请注意,如果此操作失败,则可能已成功创建了一些必要的父目录。

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

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