Skip to content

Instantly share code, notes, and snippets.

@ato
Created October 1, 2013 05:55
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ato/6774390 to your computer and use it in GitHub Desktop.
Save ato/6774390 to your computer and use it in GitHub Desktop.
TempDirectory java temporary directory delete on exit
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
class TempDirectory {
final Path path;
public TempDirectory() {
try {
path = Files.createTempDirectory("amberdb");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public Path getPath() {
return path;
}
public void deleteOnExit() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
delete();
}
});
}
public void delete() {
if (!Files.exists(path)) {
return;
}
try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
Files.deleteIfExists(dir);
return super.postVisitDirectory(dir, exc);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
Files.deleteIfExists(file);
return super.visitFile(file, attrs);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment