All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.databasesandlife.util.TemporaryFile Maven / Gradle / Ivy

There is a newer version: 21.0.1
Show newest version
package com.databasesandlife.util;

import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;

/**
 * Represents a temporary file.
 *   

* Difference to simply creating a temporary file is that this can be used in a try block and is deleted afterwards. *

* Usage: *

 *     try (var f = new TemporaryFile("myfile", "jpg")) {
 *         ...
 *     } // deleted here
 * 
* * @author This source is copyright Adrian Smith and licensed under the LGPL 3. * @see Project on GitHub */ public class TemporaryFile implements AutoCloseable { public final @Nonnull File file; public TemporaryFile(@Nonnull String prefix, @Nonnull String extension) { try { file = File.createTempFile(prefix, "."+extension); } catch (IOException e) { throw new RuntimeException(e); } } @SuppressWarnings("ResultOfMethodCallIgnored") @Override public void close() { file.delete(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy