Java - Directory Create by File Object

Introduction

You can use the mkdir() or mkdirs() method to create a new directory.

The mkdir() method creates a directory if the parent directories exists.

File newDir = new File("C:\\users\\home");

mkdirs() method will create the directory and its parents directories if it does not exist.

Demo

import java.io.File;

public class Main {
  public static void main(String[] args) {

    File newDir = new File("C:\\users-1\\home");
    boolean created = newDir.mkdir();
    System.out.println(created);//from w w  w.ja v a  2s .  c  o  m

    created = newDir.mkdirs();
    System.out.println(created);
  }
}

Result

Related Topic