Q:

How to add sub directory under parent directory in azure in java ?

I am creating directories in azure storage account by java services.
jSON input is : 
{   "accountName" : "name", 
    "accountkey"  : "keyOfAzureAccount",

    "directoryStructure" : "directory1/directory2/directory3/directory4/directory5"
}

What I am expecting is to create these directories one-under-one in azure account. Like directory5 will be inside directory4. directory4 will be inside directory3. directory3 will be inside directory2 and directory2 will be inside directory1.
My java code is like :
@Override
    public JSONObject createDynamicDirectory(JSONObject jsonInput) throws InvalidKeyException, URISyntaxException {
        CloudFileClient fileClient = null;
        String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName="+jsonInput.get("accountName")+";"+"AccountKey="+jsonInput.get("accountkey");
        System.out.println(storageConnectionString);
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        JSONObject jsonOutput = new JSONObject();
        try {
            fileClient = storageAccount.createCloudFileClient();
            String directoryName = jsonInput.get("directoryStructure").toString();

            String[] directoryNameArray = directoryName.split("\\s*/\\s*");
            System.out.println(directoryNameArray.length);

            CloudFileShare share = fileClient
                    .getShareReference(directoryNameArray[0].toLowerCase().replaceAll("[-+.^:,!@#$%&*()_~`]", ""));
            if (share.createIfNotExists()) {
                System.out.println("New share created named as "
                        + directoryName.toLowerCase().replaceAll("[-+.^:,!@#$%&*()_~`]", ""));
            }
            for(int i=0;i<directoryNameArray.length;i++)
            {
                CloudFileDirectory rootDir = share.getRootDirectoryReference();
                CloudFileDirectory parentDirectory = rootDir.getDirectoryReference(directoryNameArray[i]);
                if (parentDirectory.createIfNotExists()) {
                    System.out.println("new directory created named as " + directoryName);
                    jsonOutput.put("status", "successful");
                }
            }

        } catch (Exception e) {
            System.out.println("Exception is " + e);
            jsonOutput.put("status", "unsuccessful");
            jsonOutput.put("exception", e.toString());
        }
        return jsonOutput;
    }
}

This code creates share from directory1 as required. But the problem is, under the same share , it creates all directories1,2,3,4,5. Not like one-under-one directory as required.
How can I implement my java code so that directories can be created as required?

A:

Try something like this (the code is in C#)
        var parentDirectory = share.GetRootDirectoryReference();
        for (var i=1; i< directoryNameArray.Length; i++)
        {
            var directoryToCreate = directoryNameArray[i];
            var directory = parentDirectory.GetDirectoryReference(directoryToCreate);
            directory.CreateIfNotExists();
            Console.WriteLine("Created directory - " + directoryToCreate);
            parentDirectory = directory;
        }

Essentially you start with the share as root directory and as you start creating child directories you keep on updating the reference for root directory.

