Package com._1c.g5.v8.dt.common
Class FileUtil.AutoDeleteFolder
java.lang.Object
com._1c.g5.v8.dt.common.FileUtil.AutoDeleteFolder
- All Implemented Interfaces:
AutoCloseable
- Enclosing class:
- FileUtil
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 Summary
ConstructorsConstructorDescriptionAutoDeleteFolder
(File folder) Creates new instance.AutoDeleteFolder
(File folder, Function<File, Boolean> deleteDecisionMaker) Creates new instance that optionally leaves directory intact without deleting it. -
Method Summary
-
Constructor Details
-
AutoDeleteFolder
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 benull
.deleteDecisionMaker
- Determines if the folder should be deleted or not. The decision is made duringclose()
rather than when this instance is constructed. This way it is possible to make the decision using after-effects. Decision maker itself must not benull
, will receive non-null
directory for which to make decision and should returnBoolean#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 duringclose()
.
-
AutoDeleteFolder
Creates new instance.- Parameters:
folder
- The folder to be deleted when this instance is auto-closed. Must not benull
.
-
-
Method Details
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
-
getFolder
Gets folder represented by this instance.- Returns:
- The folder that will be automatically deleted when this instance is closed. Never
null
.
-