/** * Create directory that is optionally cleaned up after the * JVM shuts down through use of a Runtime shutdown hook. * * @param prefix Prefix to apply to temp directory name * @param cleanup true/false * @return File handle to the created temp directory * @throws IOException Unable to create temp directory */ public static File createTempDir(String prefix, boolean cleanup) throws IOException { Path tmpDir = Files.createTempDirectory(prefix); addCleanupHook(tmpDir); return tmpDir.toFile(); }
/** * Create directory that is automatically cleaned up after the * JVM shuts down through use of a Runtime shutdown hook. * * @param dir Directory to create, including missing parent directories * @param cleanup true/false * @return File handle to the created temp directory */ public static File createTempDir(File dir, boolean cleanup) throws IOException { if (!dir.mkdirs() && !dir.exists()) { throw new IOException(String.format("Failed to create directory structure '%s'", dir.toString())); } if (cleanup) { addCleanupHook(dir.toPath()); } return dir; }