StevenMurphy
StevenMurphy

Reputation: 17

Java Create a Directory that does not exist

I am currently working on RecycleBin component for a file management server. When a document gets deleted all versions get deleted from multiple locations. When this occurs all folder paths get deleted to a repeating string dp_original_folder_paths.

I want to creat the folder path when it doesnt exist.

Here is my current code. The first part checks if file exists if not creates it. The second part shows the document being relinked to folder.

for (int i = 0;
            i < relationRecord.getValueCount("dp_original_folder_paths"); i++)
            {
                File f = new File(relationRecord.getRepeatingString(
                        "dp_original_folder_paths",
                        i));

                if(!f.exists())
                {
                    System.out.println("creating directory" + f);
                    f.mkdir();
                }
// Link the document back to the original folders

    for (int i = 0; i < relationRecord.getValueCount("dp_original_folder_paths")
                i++)
     {

    document.link(
        relationRecord.getRepeatingString("dp_original_folder_paths", i));

    }

The output given is

creating directory: \EAM\sbotest

DIR created

Linked to /EAM/sbotest

DfPathNotFoundException:: THREAD: http-bio-8080-exec-7; MSG: [DM_API_E_EXIST]err or: "Folder specified by /EAM/sbotest does not exist."; ERRORCODE: 100; NEXT: null

Does anyone know why in the first println the output is \EAM\sbotest and the second output the \ become / /EAM/sbotest

Thanks for any help.

Upvotes: 1

Views: 276

Answers (3)

devops
devops

Reputation: 9197

since Java 1.7 it's recommended to use

Files.createDirectories(new File("C:/dir1/dir2/dir3/").toPath());

Files (Java Platform SE 7 )

Upvotes: 2

Steven
Steven

Reputation: 271

Have you looked into File#mkdirs?

It will create the directory and any non-existent parent directories for you.

Upvotes: 0

Laurynas Tretjakovas
Laurynas Tretjakovas

Reputation: 659

use f.mkdirs() to create parent directories as well. f.mkdir() will only try to create the last child directory, parent ones must exist.

Upvotes: 0

Related Questions