Package org.apache.poi.util
Class TempFile
- java.lang.Object
-
- org.apache.poi.util.TempFile
-
public final class TempFile extends Object
Interface for creating temporary files. Collects them all into one directory by default.
-
-
Field Summary
Fields Modifier and Type Field Description static StringJAVA_IO_TMPDIRDefine a constant for this property as it is sometimes mistyped as "tempdir" otherwise
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static FilecreateTempDirectory(String name)static FilecreateTempFile(String prefix, String suffix)Creates a new and empty temporary file.static voidsetTempFileCreationStrategy(TempFileCreationStrategy strategy)Configures the strategy used bycreateTempFile(String, String)to create the temporary files.static voidsetThreadLocalTempFileCreationStrategy(TempFileCreationStrategy strategy)Configures the strategy used bycreateTempFile(String, String)to create the temporary files.static <R> RwithStrategy(TempFileCreationStrategy newStrategy, Supplier<? extends R> task)Executes the given task ensuring that POI will use the given temp file creation strategy within the scope of the given task.
-
-
-
Field Detail
-
JAVA_IO_TMPDIR
public static final String JAVA_IO_TMPDIR
Define a constant for this property as it is sometimes mistyped as "tempdir" otherwise- See Also:
- Constant Field Values
-
-
Method Detail
-
setTempFileCreationStrategy
public static void setTempFileCreationStrategy(TempFileCreationStrategy strategy)
Configures the strategy used bycreateTempFile(String, String)to create the temporary files.- Parameters:
strategy- The new strategy to be used to create the temporary files.- Throws:
IllegalArgumentException- When the given strategy isnull.
-
setThreadLocalTempFileCreationStrategy
public static void setThreadLocalTempFileCreationStrategy(TempFileCreationStrategy strategy)
Configures the strategy used bycreateTempFile(String, String)to create the temporary files.- Parameters:
strategy- The new strategy to be used to create the temporary files for this thread.nullcan be used to reset the strategy for this thread to the default one.- Since:
- POI 5.5.0
-
createTempFile
public static File createTempFile(String prefix, String suffix) throws IOException
Creates a new and empty temporary file. By default, files are collected into one directory and are not deleted on exit from the VM, although they can be deleted by defining the system propertypoi.delete.tmp.files.on.exit(seeDefaultTempFileCreationStrategy), which may causeOutOfMemoryErrorproblem due to the frequent usage ofFile.deleteOnExit()Don't forget to close all files or it might not be possible to delete them.
- Parameters:
prefix- The prefix to be used to generate the name of the temporary file.suffix- The suffix to be used to generate the name of the temporary file.- Returns:
- The path to the newly created and empty temporary file.
- Throws:
IOException- If no temporary file could be created.
-
createTempDirectory
public static File createTempDirectory(String name) throws IOException
- Throws:
IOException
-
withStrategy
public static <R> R withStrategy(TempFileCreationStrategy newStrategy, Supplier<? extends R> task)
Executes the given task ensuring that POI will use the given temp file creation strategy within the scope of the given task. The change of strategy is not visible to other threads, and the previous strategy is restored after the task completed (normally or exceptionally).- Parameters:
newStrategy- the temp file strategy to be used in the scope of the given tasktask- the task to be executed with the given temp file strategy- Returns:
- the result of the given task
- Since:
- POI 5.5.0
-
-