YouTip LogoYouTip

Java Nio File Createdirectories

[![Image 1: Java File](#) Java java.nio.file.Files](#)


java.nio.file.Files.createDirectories() is a utility method provided in the Java NIO (New I/O) package for creating multi-level directory structures. Unlike the createDirectory() method, createDirectories() automatically creates all non-existent parent directories in the path.

Method Definition

public static Path createDirectories(Path dir, FileAttribute<?>... attrs)throws IOException

Parameter Description

Path dir

  • Directory path to create
  • Type: java.nio.file.Path
  • Cannot be null

FileAttribute<?>... attrs (Optional)

  • Optional parameter for setting directory attributes
  • Type: Variable argument FileAttribute<?>
  • Can specify file permissions, owner and other attributes

Return Value

  • Returns the Path object of the created directory
  • If the directory already exists, returns the Path object of that directory

Exception Handling

IOException

  • Thrown when unable to create directory
  • Common reasons:
    • Insufficient permissions
    • File system is full
    • A name in the path is an existing file rather than a directory

FileAlreadyExistsException

  • Thrown when a name in the path is an existing file rather than a directory
← Java Nio File CreatetempdirectJava Nio File Newdirectorystre β†’