Creating Regular Files – Java I/O: Part II

Creating Regular Files

The createFile() method of the Files class fails to create a regular file if the file already exists, or the parent directories on the path do not exist. The code below at (1) creates a file with the path project/docs/readme.txt relative to the current directory under the assumption that the file name does not exist and the parent directories exist on the path. However, running this code repeatedly will result in a FileAlreadyExistsException, unless the file is deleted (e.g., calling Files.deleteIfExists(regularFile)) before rerunning the code.

Click here to view code image

try {
  Path regularFile  = Path.of(“project”, “docs”, “readme.txt”);
  Path createdFile1 = Files.createFile(regularFile, fileAttr);        // (1)
  FileUtils.printDirEntryInfo(createdFile1);
} catch (NoSuchFileException | FileAlreadyExistsException fe) {
  fe.printStackTrace();
} catch (IOException ioe) {
  ioe.printStackTrace();
}

Possible output from the code:

File: project/docs/readme.txt
rw-r–r–

Creating Temporary Files

Programs usually create temporary files during execution, and delete such files when done in the interest of good housekeeping. The JVM defines a system property for the default temporary-file directory where temporary files are created, if a specific location is not specified. This location is printed by the code below at (1).

How the name of the temporary file is generated is file-system specific. The code at (2) creates a temporary file under the default temporary-file directory, that has default file permissions.

The code at (3) creates temporary files under a specific directory. How the specification of file name prefix and suffix affect the generated file name can be seen in the output, where the null value as the prefix omits the prefix, and the null value as the suffix appends the default file name extension “.tmp”.

The NIO.2 API does not define a method to request that a file be deleted when the JVM terminates.

Click here to view code image

try {
  // System property that defines the default temporary-file directory.      (1)
  String tmpdir = System.getProperty(“java.io.tmpdir”);
  System.out.println(“Default temporary directory: ” + tmpdir);
  // Create under the default temporary-file directory.                      (2)
  Path tmpFile1 = Files.createTempFile(“events”, “.log”);
  FileUtils.printDirEntryInfo(tmpFile1);
  // Create under a specific directory:                                      (3)
  Path tmpFileDir = Path.of(“project”);
  Path tmpFile2 = Files.createTempFile(tmpFileDir, “proj_”, “.dat”, fileAttr);
  Path tmpFile3 = Files.createTempFile(tmpFileDir, “proj_”, null,   fileAttr);
  Path tmpFile4 = Files.createTempFile(tmpFileDir, null,    “.dat”, fileAttr);
  Path tmpFile5 = Files.createTempFile(tmpFileDir, null,    null,   fileAttr);
  FileUtils.printDirEntryInfo(tmpFile2);
  FileUtils.printDirEntryInfo(tmpFile3);
  FileUtils.printDirEntryInfo(tmpFile4);
  FileUtils.printDirEntryInfo(tmpFile5);
} catch (IOException ioe) {
  ioe.printStackTrace();
}

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

Click here to view code image

Default temporary directory: /var/folders/cr/wk7fqcjx07z95d9vxcgjnrtc0000gr/T/
File:
/var/folders/cr/wk7fqcjx07z95d9vxcgjnrtc0000gr/T/events4720093907665196131.log
rw——-
File: project/proj_6062790522710209175.dat
rw-r–r–
File: project/proj_957015125593453845.tmp
rw-r–r–
File: project/3032983609251590109.dat
rw-r–r–
File: project/8205471872222375044.tmp
rw-r–r–

Categories: ,

Leave a Reply

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