Class TemporaryFileResource

java.lang.Object
io.micronaut.core.io.file.TemporaryFileResource
All Implemented Interfaces:
Closeable, AutoCloseable

@Internal public final class TemporaryFileResource extends Object implements Closeable
Closeable resource class representing a temporary file. When this resource is closed, the file is deleted.
Since:
5.0.0
Author:
Jonas Konrad
  • Constructor Details

    • TemporaryFileResource

      public TemporaryFileResource(Path path)
      Create a new file resource. Ownership of the path is transferred to the resource.
      Parameters:
      path - The path
  • Method Details

    • getPath

      public Path getPath()
      Get the path.
      Returns:
      The path
      Throws:
      IllegalStateException - if this resource is closed or has been moved
    • moveResource

      public TemporaryFileResource moveResource()
      Move this resource. The returned resource will have ownership of the path, and the original resource will lose ownership, meaning it is essentially closed. No actual file system operation is done.
      Returns:
      The new resource managing this path
      Throws:
      IllegalStateException - if this resource is already closed or has been moved
    • moveFile

      public void moveFile(Path destination) throws IOException
      Move this file to a new location. This resource loses ownership of the file, you'll have to manage it from now on.
      Parameters:
      destination - The destination to move to
      Throws:
      IOException - If the file cannot be moved. Note that the file will be deleted in this case, the resource still becomes invalid
    • isOpen

      public boolean isOpen()
      Check whether this resource is still open and has ownership of the path.
      Returns:
      true if this resource is still open
    • close

      public void close() throws IOException
      Close this resource, deleting the underlying file. If this resource is already closed or has been moved, this method does nothing.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - Failure deleting the file