how to create folder java

how to create folder java

To create a folder in Java, you can use the mkdirs method of the File class. This method creates the specified directory, as well as any necessary parent directories, if they don't already exist.

Here is an example of how you can create a folder in Java:

r‮ot refe‬:lautturi.com
import java.io.File;

public class Main {
  public static void main(String[] args) {
    // Create a File object for the directory to be created
    File dir = new File("/path/to/new/directory");

    // Create the directory
    boolean success = dir.mkdirs();

    if (success) {
      System.out.println("Directory created successfully");
    } else {
      System.out.println("Error creating directory");
    }
  }
}

In this example, we create a File object for the directory to be created, and then call the mkdirs method on it. The mkdirs method returns a boolean value indicating whether the directory was created successfully or not. If the directory was created successfully, the if statement will print "Directory created successfully" to the console; otherwise, it will print "Error creating directory".

Note that the mkdirs method only creates directories, not files. To create a file, you can use the createNewFile method of the File class.

Created Time:2017-11-01 20:42:48  Author:lautturi