简体   繁体   中英

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.

We don't know enough to be sure that this is the answer, but...

mkdir relies on the parent directory already existing. So it won't be able to create the Ranjeet directory unless ECR-DZ-00013 already exists.

However, you can use mkdirs instead which creates all the intermediate directories as required:

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.

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