Create folder in Java

You can create a folder using the File class. Here's an example code snippet that demonstrates how to create a folder:

import java.io.File; public class CreateFolderExample { public static void main(String[] args) { String folderName = "myFolder"; // name of the folder to create File folder = new File(folderName); if (!folder.exists()) { boolean created = folder.mkdir(); if (created) { System.out.println("Folder created successfully."); } else { System.out.println("Failed to create folder."); } } else { System.out.println("Folder already exists."); } } }


In this example, we first create a File object with the name of the folder we want to create. We then check if the folder already exists. If it does not exist, we create the folder using the mkdir() method. The mkdir() method returns a boolean value that indicates whether the folder was created successfully. Finally, we print a message indicating whether the folder was created successfully or not.


Note that the mkdir() method only creates a single directory. If you need to create multiple directories (a directory tree), you can use the mkdirs() method instead.