TemporaryResources implements Closeable { public Path createTempFile() throws IOException { final Path path = tempFileDir == null ? Files.createTempFile("apache-tika-", ".tmp") : Files.createTempFile(tempFileDir, "apache-tika-", ".tmp"); addResource(new Closeable() { public void close() throws IOException { try { Files.delete(path); } catch (IOException e) { LOG.warn("delete tmp file fail, will delete it on exit"); path.toFile().deleteOnExit(); } } }); return path; }  void setTemporaryFileDirectory(Path tempFileDir); void setTemporaryFileDirectory(File tempFileDir); Path createTempFile(); File createTemporaryFile(); void addResource(Closeable resource); @SuppressWarnings("unchecked") T getResource(Class<T> klass); void close(); void dispose();  }
@Test public void testFileDeletion() throws IOException { Path tempFile; try (TemporaryResources tempResources = new TemporaryResources()) { tempFile = tempResources.createTempFile(); assertTrue("Temp file should exist while TempResources is used", Files.exists(tempFile)); } assertTrue("Temp file should not exist after TempResources is closed", Files.notExists(tempFile)); }