Java Create Directory Examples

This article has examples about how to create a directory use java.io.File, java.nio.file.Files and org.apache.commons.io.FileUtils. If you do not know how to add Apache commons io jar file into your java project, please read article Copy Directory Examples.

1. Create a folder like C:/WorkSpace/dev2qa, paretn folder C:/WorkSpace is exist.

  1. Use java.io.File.mkdir().
    	/* Create a directory. */
    	public void createDir(String dirPath)
    	{
    		if(dirPath!=null && !"".equals(dirPath.trim()))
    		{
    			File dirFile = new File(dirPath);
    			
    			if(!dirFile.exists())
    			{
    				boolean result = dirFile.mkdir();
    				if(result)
    				{
    					System.out.println("Create " + dirPath + " success. ");
    				}else
    				{
    					System.out.println("Create " + dirPath + " fail. ");
    				}
    			}else
    			{
    				System.out.println(dirPath + " exist. ");
    			}
    		}
    	}
  2. Use java.nio.file.Files.createDirectory(Path path).
    	public void crateDirUseJavaNewIO(String dirPath)
    	{
    		try
    		{
    			if(dirPath!=null && !"".equals(dirPath.trim()))
    			{
    				Path pathObj = Paths.get(dirPath);
    		        
    		        if (Files.notExists(pathObj)) {
    		           Files.createDirectory(pathObj); 
    		           System.out.println("Create " + dirPath + " success. ");
    		        }else
    		        {
    		        	System.out.println(dirPath + " exist. ");
    		        }
    			}
    		}catch(IOException ex)
    		{
    			ex.printStackTrace();
    		}
    	}
    
  3. Use org.apache.commons.io.FileUtils.forceMkdir(File dirFile). This method can also create a folder tree like C:/WorkSpace/dev2qa/temp/temp1/temp2, only root parent folder C:/WorkSpace exist, other folder are all not exist.
    	public void crateDirTreeUseApacheCommonsIO(String dirPath)
    	{
    		try
    		{	
    			if(dirPath!=null && !"".equals(dirPath.trim()))
    			{
    				File dirFileObj = FileUtils.getFile(dirPath);
    		        
    		        if (!dirFileObj.exists()) {
    		           FileUtils.forceMkdir(dirFileObj); 
    		           System.out.println("Create " + dirPath + " success. ");
    		        }else
    		        {
    		        	System.out.println(dirPath + " exist. ");
    		        }
    			}
    		}catch(IOException ex)
    		{
    			ex.printStackTrace();
    		}
    	}
    

2. Create folder tree like C:/WorkSpace/dev2qa/temp/temp1/temp2, only paretn folder C:/WorkSpace exist, other folder are all not exist.

  1. Use java.io.File.mkdirs().
    	/* Create a directory and it's none exist parent directory. */
    	public void createDirTree(String dirPath)
    	{
    		if(dirPath!=null && !"".equals(dirPath.trim()))
    		{
    			File dirFile = new File(dirPath);
    			
    			if(!dirFile.exists())
    			{
    				boolean result = dirFile.mkdirs();
    				if(result)
    				{
    					System.out.println("Create " + dirPath + " success. ");
    				}else
    				{
    					System.out.println("Create " + dirPath + " fail. ");
    				}
    			}else
    			{
    				System.out.println(dirPath + " exist. ");
    			}
    		}
    	}
    
  2. Use java.nio.file.Files.createDirectories(Path path).
    	public void crateDirTreeUseJavaNewIO(String dirPath)
    	{
    		try
    		{
    			if(dirPath!=null && !"".equals(dirPath.trim()))
    			{
    				Path pathObj = Paths.get(dirPath);
    		        
    		        if (Files.notExists(pathObj)) {
    		           Files.createDirectories(pathObj); 
    		           System.out.println("Create " + dirPath + " success. ");
    		        }else
    		        {
    		        	System.out.println(dirPath + " exist. ");
    		        }
    			}
    		}catch(IOException ex)
    		{
    			ex.printStackTrace();
    		}
    	}

3. Java main method to call above methods.

	public static void main(String[] args) {
		
		CreateDirectoryExample cde = new CreateDirectoryExample();
		
		cde.createDir("C:/WorkSpace/dev2qa");
		
		cde.createDirTree("C:/WorkSpace/dev2qa/temp1/temp2");
		
		cde.crateDirUseJavaNewIO("C:/WorkSpace/dev2qa1/");
		
		cde.crateDirTreeUseJavaNewIO("C:/WorkSpace/dev2qa1/temp2/temp3");
		
		cde.crateDirTreeUseApacheCommonsIO("C:/WorkSpace/dev2qa2/temp2/temp3");
		
	}

[download id=”2223″]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.