Class TempFile.TempFileBuilder

  • Enclosing class:
    TempFile

    public static final class TempFile.TempFileBuilder
    extends Object
    • Method Detail

      • dir

        @CanIgnoreReturnValue
        public TempFile.TempFileBuilder dir​(Path pDir)
        The directory where the file will be created, default is JVM's temp directory.
      • initialContent

        @CanIgnoreReturnValue
        public TempFile.TempFileBuilder initialContent​(Object pContent,
                                                       Charset pCharset)
        Content to write to temp file immediately after creation.
      • create

        public Path create()
                    throws IOException
        Create a fresh temporary file according to the specifications set on this builder.

        If the temporary file should be removed after some specific code is executed, use createDeleteOnClose().

        This instance can be safely used again afterwards.

        Throws:
        IOException
      • createDeleteOnClose

        public TempFile.DeleteOnCloseFile createDeleteOnClose()
                                                       throws IOException
        Create a fresh temporary file according to the specifications set on this builder.

        The resulting Path object is wrapped in a TempFile.DeleteOnCloseFile, which deletes the file as soon as TempFile.DeleteOnCloseFile.close() is called.

        It is recommended to use the following pattern: try (DeleteOnCloseFile tempFile = TempFile.builder()[.. adjust builder ..].createDeleteOnClose()) { // use tempFile.toPath() for writing and reading of the temporary file } The file can be opened and closed multiple times, potentially from different processes.

        This instance can be safely used again afterwards.

        Throws:
        IOException