Creating Regular Directories – Java I/O: Part II

Creating Regular Directories

The Files class provides two methods to create regular directories. The create-Directory() method fails to create a regular directory if the directory already exists, or if the parent directories on the path do not exist. The code below creates a directory with the path project/bin relative to the current directory, under the assumption that the directory name bin does not exist and the parent directory ./project exists on the path. The directory is first created with the default file permissions at (1), then deleted at (2) and created again at (3) with specific file permissions. The second call to the createDirectory() method at (3) would throw a FileAlreadyExistsException if we did not delete the directory before creating it with specific file permissions.

The astute reader will have noticed from the output that the write permission for the group and others is not set at creation time by the createDirectory() method, regardless of the file permissions specified. This can be remedied by setting the file permissions explicitly after the directory has been created, as at (4).

Click here to view code image

try {
  Path regularDir  = Path.of(“project”, “bin”);
  Path createdDir = Files.createDirectory(regularDir);                    // (1)
  FileUtils.printDirEntryInfo(createdDir);
  if (Files.deleteIfExists(regularDir)) {                                 // (2)
    System.out.printf(“Directory deleted: %s%n”, regularDir);
  }
  Path newDir = Files.createDirectory(regularDir, dirFileAttr);           // (3)
  FileUtils.printDirEntryInfo(newDir);
  Files.setPosixFilePermissions(newDir, dPerms);                          // (4)
  FileUtils.printDirEntryInfo(newDir);
} catch (NoSuchFileException | FileAlreadyExistsException fe) {
  fe.printStackTrace();
} catch (IOException ioe) {
  ioe.printStackTrace();
}

Possible output from the code:

Click here to view code image

Directory: project/bin
rwxr-xr-
Directory deleted: project/bin
Directory: project/bin
rwxr-xr-x
Directory: project/bin
rwxrwxrwx

The method createDirectories() first creates all nonexistent parent directories in the path if necessary, and only creates the directory if it does not exist—a convenient way to ensure that a directory always exists. In the code below at (5), the parent directories (in the path project/branches/maintenance) are created from top to bottom, relative to the current directory if necessary, and the directory versions is only created if it does not exist from before. A FileAlreadyExistsException at (6) is thrown if a directory entry named versions exists, but is not a directory.

Click here to view code image

try {
  Path regularDir2  = Path.of(“project”, “branches”, “maintenance”, “versions”);
  Path createdDir2 = Files.createDirectories(regularDir2, dirFileAttr);  // (5)
  FileUtils.printDirEntryInfo(createdDir2);
} catch (FileAlreadyExistsException fe) {                                // (6)
  fe.printStackTrace();
} catch (IOException ioe) {
  ioe.printStackTrace();
}

Output from the code:

Click here to view code image

Directory: project/branches/maintenance/versions
rwxrwxrwx

Leave a Reply

Your email address will not be published. Required fields are marked *