Class TempFile

java.lang.Object
de.xima.fc.io.TempFile
All Implemented Interfaces:
Serializable, AutoCloseable

public final class TempFile extends Object implements AutoCloseable, Serializable
An AutoCloseable wrapper for a File that deletes that file when closed.
Author:
XIMA MEDIA GmbH
See Also:
  • Constructor Details

    • TempFile

      public TempFile(File file)
      Creates a new temp file. The given file will be deleted when this instance if closed.
      Parameters:
      file - File to wrap.
    • TempFile

      public TempFile(Path file)
      Creates a new temp file. The given file will be deleted when this instance if closed.
      Parameters:
      file - File to wrap.
  • Method Details

    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • getFile

      public File getFile()
      Returns:
      The wrapped file.
    • getPath

      public Path getPath()
      Returns:
      The wrapped file.
    • create

      public static TempFile create(Path directory, String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
      Creates a temporary file, wraps it in a TempFile and returns it. This method is a convenience method for wrapping a temporary file created with Files.createTempFile(Path, String, String, FileAttribute[]).
      Parameters:
      directory - the path to directory in which to create the file
      prefix - The prefix string to be used in generating the file's name; may be null.
      suffix - The suffix string to be used in generating the file's name; may be null, in which case ".tmp" is used
      attrs - An optional list of file attributes to set atomically when creating the file
      Returns:
      The newly created temp file.
      Throws:
      IllegalArgumentException - If the prefix or suffix parameters cannot be used to generate a candidate file name.
      UnsupportedOperationException - If the array contains an attribute that cannot be set atomically when creating the directory
      IOException - If an I/O error occurs or dir does not exist.
      SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to the file.
    • create

      public static TempFile create(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
      Creates a temporary file, wraps it in a TempFile and returns it. This method is a convenience method for wrapping a temporary file created with Files.createTempFile(String, String, FileAttribute[]).
      Parameters:
      prefix - The prefix string to be used in generating the file's name; may be null.
      suffix - The suffix string to be used in generating the file's name; may be null, in which case ".tmp" is used
      attrs - An optional list of file attributes to set atomically when creating the file
      Returns:
      The newly created temp file.
      Throws:
      IllegalArgumentException - If the prefix or suffix parameters cannot be used to generate a candidate file name.
      UnsupportedOperationException - If the array contains an attribute that cannot be set atomically when creating the directory
      IOException - If an I/O error occurs or dir does not exist.
      SecurityException - In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to the file.