Path path = Paths.get("myFolder"); try { Files.createDirectories(path); } catch (IOException e) { System.err.println("Error creating directory: " + e.getMessage()); }
Path parentPath = Paths.get("parentFolder"); Path childPath = parentPath.resolve("myFolder"); try { Files.createDirectories(childPath); } catch (IOException e) { System.err.println("Error creating directory: " + e.getMessage()); }Both examples use the Files.createDirectories method to create a directory or a directory tree. The method returns a Path object representing the created directory or directories. The IOException is thrown if there is an error creating the directory. The Files class that contains createDirectories method is located in the java.nio.file package which is a part of the Java standard library.