Creating Temporary Directories – Java I/O: Part II

Creating Temporary Directories

Analogous to temporary files, temporary directories can be created by the create-TempDirectory() methods of the Files class. Again, these directories can be created in the default temporary-file directory, or in a specific location if one is specified in the method call, with either the default file permissions or specific permissions.

The code at (7) creates a temporary directory named “log_dir” under the default temporary-file directory. A temporary directory with the same name is also created under a specific location (the./project directory) at (8). A NoSuchFileException is thrown if the specified location does not exist. A prefix can be specified for the directory name or it can be null.

Click here to view code image

try  {
  // Create under the default temporary-file directory.                     (7)
  Path tmpDirPath1 = Files.createTempDirectory(“log_dir”, dirFileAttr);
  FileUtils.printDirEntryInfo(tmpDirPath1);
  // Create under a specific location:                                      (8)
  Path tmpDirLoc = Path.of(“project”);
  Path tmpDirPath2 = Files.createTempDirectory(tmpDirLoc, “log_dir”, dirFileAttr);
  Path tmpDirPath3 = Files.createTempDirectory(tmpDirLoc, null, dirFileAttr);
  FileUtils.printDirEntryInfo(tmpDirPath2);
  FileUtils.printDirEntryInfo(tmpDirPath3);
  Files.setPosixFilePermissions(tmpDirPath3, dPerms);                    // (9)
  FileUtils.printDirEntryInfo(tmpDirPath3);
} catch (NoSuchFileException nsfe) {
  nsfe.printStackTrace();
} catch (IOException ioe) {
  ioe.printStackTrace();
}

Possible output from the code (edited to fit the page):

Click here to view code image

Directory:
/var/folders/cr/wk7fqcjx07z95d9vxcgjnrtc0000gr/T/log_dir18136008118052819366
rwxr-xr-x
Directory: project/log_dir14908011762674796217
rwxr-xr-x
Directory: project/18428782809319921018
rwxr-xr-x
Directory: project/18428782809319921018
rwxrwxrwx

We notice from the output that the write permission for the group and others is not set at creation time by the createTempDirectory() method, regardless of the file permissions specified. Again, this can be remedied by setting the file permissions explicitly after the temporary directory has been created, as at (9).

21.8 Stream Operations on Directory Entries

The Files class provides static methods that create specialized streams to implement complex file operations on directory entries, which are concise and powerful, taking full advantage of the Stream API. They are also efficient as the lazy execution of the streams ensures that an element is only made available for processing when needed by the terminal stream operation.

Streams we have seen earlier do not use any system resources, as they are backed by data structures and generator functions. Such streams need not be closed, as they are handled as any other objects by the runtime environment with regard to memory management. In contrast, the streams that are backed by file system resources must be closed in order to avoid resource leakage—analogous to using I/O streams. As streams implement the AutoCloseable interface, the recommended practice is to use the try-with-resources statement, ensuring proper and prompt closing of file system resources after the stream operations complete.

Methods that create streams on directory entries throw a checked IOException, and in particular, they throw a NoSuchFileException if the directory entry does not exist. Any code using these methods is forced to handle these exceptions with either a try-catch-finally construct or a throws clause.

The examples in this section illustrate both closing of streams and handling of exceptions.

Leave a Reply

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