How to create directory along with parent directory


 * Here we will learn to create directory along with non existent parent Directory
public class CreateDirWithNonExistentParentDir {

	public static void main(String args[]) {

		// Here we are assuming that D:\Gautam\testing folder exists but first
		// and second folder is not there and user
		// has privilege to create directory in testing folder
		File file = new File("D:\JBT\testing\first\second");

		boolean b = false;

		 * exists() method tests whether the file or directory denoted by this
		 * abstract pathname exists or not accordingly it will return TRUE /
		 * FALSE.

		if (!file.exists()) {
			 * mkdirs() method creates the directory mentioned by this abstract
			 * pathname including any necessary but nonexistent parent
			 * directories.
			 * Accordingly it will return TRUE or FALSE if directory created
			 * successfully or not. If this operation fails it may have
			 * succeeded in creating some of the necessary parent directories.
			b = file.mkdirs();
		if (b)
			System.out.println("Directory successfully created");
			System.out.println("Failed to create directory");


By | 2012-09-21T19:39:14+00:00 September 21st, 2012|Code Base|3 Comments

About the Author:


  1. Kumar February 16, 2016 at 12:50 pm - Reply

    What will CanWrite return if folder does not exists.

  2. Shashi Ranjan June 21, 2016 at 5:13 am - Reply

    File outFile = new File(fileName);

  3. Asma July 16, 2017 at 3:28 pm - Reply

    Thnx a lot that really helped me 🙂

Leave A Comment