Create Directory Subdirectory Java

Create Directory Subdirectory Java

In this below sample program we will create directory then multiple sub directories inside it. Please see example below:

  • Sample java program:
package com.javahonk;

import java.io.File;

public class CreateDirectorySubDirectory {

	public static void main(String[] args) {

		File file = new File("C:\\JavaHonk\\zip\\CreateDirectory");

		if (!file.exists()) {
			file.mkdir();
			System.out.println("Directory created: "+file.getAbsolutePath());
		}else {
			System.out.println("Directory already exists: "+file.getAbsolutePath());
		}

		File parent = new File(file.getAbsolutePath() + "\\SubDirec1\\SubDirec2");
		
		if (!parent.exists()) {
			parent.mkdirs();
			System.out.println("Multiple directory created: "+parent.getAbsolutePath());
		}else {
			System.out.println("Directory already exists: "+parent.getAbsolutePath());
		}

	}

}
  • Output:

Create Directory Subdirectory Java

  • Directory structure:

Create Directory Subdirectory Java

  • For more information please refer oracle documentation here

Leave a Reply

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