FEAT FileUtils withTempDirectory + withTempFile
parent
dc04c578c9
commit
db3d60a42b
|
@ -1,8 +1,14 @@
|
|||
package de.nclazz.commons.io;
|
||||
|
||||
import lombok.NonNull;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileAttribute;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public abstract class FileUtils {
|
||||
|
||||
|
@ -31,4 +37,48 @@ public abstract class FileUtils {
|
|||
dir.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a temporary directory and passes it to {@code dirConsumer}.
|
||||
* After the consumer returns the directory will be deleted.
|
||||
*
|
||||
* Attention: never store the given {@code File} instance in an external
|
||||
* variable, as it will definately be deleted, after the {@code Consumer} returns!
|
||||
* @param dirConsumer
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static void withTempDirectory(String prefix, @NonNull Consumer<File> dirConsumer) {
|
||||
Path path = Files.createTempDirectory(prefix);
|
||||
File dir = path.toFile();
|
||||
|
||||
dirConsumer.accept(dir);
|
||||
|
||||
if(dir.exists()) {
|
||||
deleteDirectoryRecursively(dir);
|
||||
}
|
||||
}
|
||||
|
||||
public static void withTempDirectory(Consumer<File> dirConsumer) {
|
||||
withTempDirectory("nclazz", dirConsumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a temporary file and passes it to {@code fileConsumer}.
|
||||
* After the consumer returns the directory will be deleted.
|
||||
*
|
||||
* Attention: never store the given {@code File} instance in an external
|
||||
* variable, as it will definitely be deleted, after the {@code Consumer} returns!
|
||||
* @param fileConsumer
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static void withTempFile(String prefix, String suffix, @NonNull Consumer<File> fileConsumer) {
|
||||
Path path = Files.createTempFile(prefix, suffix);
|
||||
File file = path.toFile();
|
||||
|
||||
fileConsumer.accept(file);
|
||||
|
||||
if(file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package de.nclazz.commons.io;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -37,4 +36,34 @@ public class FileUtilsTests {
|
|||
return dir;
|
||||
}
|
||||
|
||||
File tempDir;
|
||||
|
||||
@Test
|
||||
void testWithTempDirectory() {
|
||||
FileUtils.withTempDirectory(dir -> {
|
||||
tempDir = dir;
|
||||
|
||||
assertTrue(dir.exists());
|
||||
assertTrue(dir.isDirectory());
|
||||
});
|
||||
|
||||
assertFalse(tempDir.isDirectory());
|
||||
assertFalse(tempDir.exists());
|
||||
}
|
||||
|
||||
File tempFile;
|
||||
|
||||
@Test
|
||||
void testWithTempFile() {
|
||||
FileUtils.withTempFile("", ".tmp", file -> {
|
||||
tempFile = file;
|
||||
|
||||
assertTrue(file.exists());
|
||||
assertFalse(file.isDirectory());
|
||||
});
|
||||
|
||||
assertFalse(tempFile.isDirectory());
|
||||
assertFalse(tempFile.exists());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue