Class FileUtil.AutoDeleteFolder

java.lang.Object
com._1c.g5.v8.dt.common.FileUtil.AutoDeleteFolder
All Implemented Interfaces:
AutoCloseable
Enclosing class:
FileUtil

public static final class FileUtil.AutoDeleteFolder extends Object implements AutoCloseable
Automatically deletes folder (or file) when leaving try-with-resources block.


 try (AutoDeleteFolder tmpFolder = new AutoDeleteFolder(FileUtil.createTempDirectory()))
 {
      tmpFolder.getFolder().createNewFile();
 }
 catch (IOException e)
 {
      // Handle creation and deletion issues.
 }
 

If the folder exists, it will be recursively deleted along with its content upon close(). If the folder does not exist, nothing will happen.

  • Constructor Details

    • AutoDeleteFolder

      public AutoDeleteFolder(File folder, Function<File,Boolean> deleteDecisionMaker)
      Creates new instance that optionally leaves directory intact without deleting it.

      This is useful if temporary folder deletion is optional but it is still desirable to use try-with-resources construct.

      Parameters:
      folder - The folder to be optionally deleted when this instance is auto-closed. Must not be null.
      deleteDecisionMaker - Determines if the folder should be deleted or not. The decision is made during close() rather than when this instance is constructed. This way it is possible to make the decision using after-effects. Decision maker itself must not be null, will receive non-null directory for which to make decision and should return Boolean#TRUE if the directory is to be deleted. Any other value will mean that no attempt to delete the directory will be made even if it exists during close().
    • AutoDeleteFolder

      public AutoDeleteFolder(File folder)
      Creates new instance.
      Parameters:
      folder - The folder to be deleted when this instance is auto-closed. Must not be null.
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException
    • getFolder

      public File getFolder()
      Gets folder represented by this instance.
      Returns:
      The folder that will be automatically deleted when this instance is closed. Never null.