How to Create Multiple Folders in Java

Create Multiple Folders in Java

This example program about, How to create multiple folders in java. The example program were tested with environment and output has been shared in the same post.

Example Program

The following program using the mkdir() method, Which is belongs to java.io.File class.

package com.dineshkrish.io;

import java.io.File;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class MultipleFolder {

	public static void main(String[] args) {

		// folder names array
		String[] folderNames = { "D", "I", "N", "E", "S", "H", "K", "R", "I", "S", "H", "N", "A", "N" };

		// iterating the string array
		for (String folderName : folderNames) {

			// defining the file object
			File file = new File(folderName);

			if (file.mkdir()) { //  creating the directory
				
				System.out.println("Folder " + folderName + " is created");
				
			} else {

				System.out.println("Folder " + folderName+ " is already exist");
			}
		}
	}
}

Output

Folder D is created
Folder I is created
Folder N is created
Folder E is created
Folder S is created
Folder H is created
Folder K is created
Folder R is created
Folder I is already exist
Folder S is already exist
Folder H is already exist
Folder N is already exist
Folder A is created
Folder N is already exist

References

1. Java File Class

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.