com.databasesandlife.util.TemporaryFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
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