Creating a subdirectory using the JNDI file system provider

Use the method createSubcontext. The following example creates a subdirectory jnditest in c:temp. If the subdirectory already exists, a javax.naming.NameAlreadyBoundException is thrown.

Main.java:

import javax.naming.*;
import java.util.*;
  
public class Main
{
   public static void main(String []args) {
      try {
         Properties properties = new Properties();
         properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
         properties.put(Context.PROVIDER_URL, "file:///");
 
         Context ctx = new InitialContext(properties);
 
         // List the objects 
         Object object = ctx.lookup("c:\temp");
  
         if (object instanceof Context) {
            Context context = (Context) object;
            context.createSubcontext("jnditest");
         }
 
         ctx.close(); 
      }
      catch(NamingException ne) {
         ne.printStackTrace();
      }
   }
}