List of usage examples for java.io File deleteOnExit
public void deleteOnExit()
From source file:de.ks.file.FileStoreTest.java
@Test public void testGetFileReferences() throws Exception { Path path = Files.createTempFile("img", ".jpg"); URL resource = getClass().getResource("/de/ks/idnadrev/entity/img.jpg"); Path src = Paths.get(resource.toURI()); Files.copy(src, path, StandardCopyOption.REPLACE_EXISTING); File file = path.toFile(); file.deleteOnExit(); FileReference fileReference = fileStore.getReference(file).get(); PersistentWork.run(em -> {/*from w w w . ja v a 2s . com*/ fileStore.scheduleCopy(fileReference, file); em.persist(fileReference); }); assertThat(fileReference.getMimeType(), containsString("image")); List<FileReference> references = fileStore.getFilesByMimeType(MediaType.ANY_IMAGE_TYPE); assertEquals(1, references.size()); references = fileStore.getFilesByMimeType(MediaType.ANY_AUDIO_TYPE); assertEquals(0, references.size()); references = fileStore.getFilesByMimeType(MediaType.ANY_TYPE); assertEquals(1, references.size()); }
From source file:com.aestasit.markdown.slidery.converters.BaseConverter.java
@SuppressWarnings("unchecked") private File joinInputFiles(Configuration config) throws IOException { File joinedFile = File.createTempFile("slidery", "joined"); joinedFile.deleteOnExit(); List<String> lines = new ArrayList<String>(); for (File inputFile : config.getInputFiles()) { lines.addAll(readLines(inputFile, config.getInputEncoding().name())); }/*from ww w .j a v a 2 s .c o m*/ writeLines(joinedFile, lines); return joinedFile; }
From source file:com.patrolpro.servlet.UploadPostOrderServlet.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from ww w . j a v a2 s. c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String companyId = request.getParameter("companyId"); String clientId = request.getParameter("clientId"); String cid = request.getParameter("cid"); FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> fields = upload.parseRequest(request); FileItem fileData = null; for (int f = 0; f < fields.size(); f++) { if (fields.get(f).getFieldName().equals("file_data")) { fileData = fields.get(f); } else if (fields.get(f).getFieldName().equals("companyId")) { companyId = fields.get(f).getString(); } else if (fields.get(f).getFieldName().equals("clientId")) { clientId = fields.get(f).getString(); } else if (fields.get(f).getFieldName().equals("cid")) { cid = fields.get(f).getString(); } } File tempFile = File.createTempFile("postinstr", "tmnp"); tempFile.deleteOnExit(); CompanyService compService = new CompanyService(); CompanyObj comp = compService.getCompanyObjById(Integer.parseInt(companyId)); FileOutputStream oStream = new FileOutputStream(tempFile); InputStream iStream = fileData.getInputStream(); byte[] buffer = new byte[2048]; int numRead = 0; while ((numRead = iStream.read(buffer)) > -1) { oStream.write(buffer, 0, numRead); } oStream.flush(); oStream.close(); iStream.close(); boolean isSuccesfull = FileLoader.saveAdditionalFile("location_additional_files", comp.getCompanyDb(), clientId, fileData.getName(), tempFile); tempFile.delete(); response.sendRedirect("/client/post_instructions.xhtml?windowId=" + cid); } catch (Exception exe) { exe.printStackTrace(); } finally { } }
From source file:com.premiumminds.persistence.utils.HibernateEnversDDLTest.java
@Test public void testUpdateCommandWithFile() throws Exception { File file = File.createTempFile("updatedb", ".sql"); file.deleteOnExit(); String[] args = new String[] { "--update", "application-data-unit-export-test", "jdbc:h2:" + getClass().getResource("/foobar_versioned.mv.db").getFile().replace(".mv.db", ""), "foo", "bar", file.getAbsolutePath() }; HibernateEnversDDL.main(args);// w w w.j a v a 2 s.c o m assertTrue(outContent.toString().isEmpty()); assertEquals( "\n" + " alter table FooBar \n" + " add column bar integer not null;\n" + "\n" + " alter table FooBar_AUD \n" + " add column bar integer;\n", IOUtils.toString(new FileReader(file))); }
From source file:eu.esdihumboldt.hale.ui.application.handler.ShowHandbookHandler.java
/** * @see IHandler#execute(ExecutionEvent) *//* w w w.j av a 2 s. com*/ @Override public Object execute(ExecutionEvent event) throws ExecutionException { String tempDirName = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$ if (!tempDirName.endsWith("/")) { //$NON-NLS-1$ tempDirName = tempDirName + "/"; //$NON-NLS-1$ } tempDirName = tempDirName + HALEApplicationPlugin.PLUGIN_ID + "/"; //$NON-NLS-1$ File tempDir = new File(tempDirName); tempDir.mkdirs(); tempDir.deleteOnExit(); File pdfFile = new File(tempDirName + PDFFILE); if (pdfFile.exists()) { FileUtils.deleteQuietly(pdfFile); } pdfFile.deleteOnExit(); URL pdfUrl = this.getClass().getResource("/documentation/" + PDFFILE); //$NON-NLS-1$ if (pdfUrl == null) { throw new RuntimeException("Manual could not be retrieved."); //$NON-NLS-1$ } InputStream in; try { in = pdfUrl.openStream(); } catch (IOException e) { ExceptionHelper.handleException("Could not open Streaming.", //$NON-NLS-1$ HALEApplicationPlugin.PLUGIN_ID, e); return null; } FileOutputStream fos = null; byte[] buffer = new byte[4096]; int read; try { fos = new FileOutputStream(pdfFile); while ((read = in.read(buffer)) != -1) { fos.write(buffer, 0, read); } } catch (IOException e) { ExceptionHelper.handleException("Error while reading the file.", //$NON-NLS-1$ HALEApplicationPlugin.PLUGIN_ID, e); return null; } finally { try { if (fos != null) { fos.close(); } in.close(); } catch (IOException e) { // ignore } } try { Desktop.getDesktop().open(pdfFile); } catch (IOException e) { ExceptionHelper.handleException("The file could not be opened", //$NON-NLS-1$ HALEApplicationPlugin.PLUGIN_ID, e); } return null; }
From source file:net.sf.jasperreports.phantomjs.ScriptManager.java
protected File copyScript(String scriptLocation, JasperReportsContext jasperReportsContext) { String resourceName = getResourceName(scriptLocation); try {/*from ww w .j a va 2s. c o m*/ File file = File.createTempFile(TEMP_FILE_PREFIX, "_" + resourceName, tempFolder); file.deleteOnExit();//TODO lucianc leak if (log.isDebugEnabled()) { log.debug("copying " + scriptLocation + " to " + file); } byte[] buf = new byte[COPY_BUFFER_SIZE]; try (InputStream input = RepositoryUtil.getInstance(jasperReportsContext) .getInputStreamFromLocation(scriptLocation); OutputStream output = new FileOutputStream(file)) { int read = 0; while ((read = input.read(buf)) > 0) { output.write(buf, 0, read); } } return file; } catch (IOException | JRException e) { throw new JRRuntimeException(e); } }
From source file:com.carlos.projects.billing.ExcelToMySQLImporter.java
private void closeAndDeleteTemporaryFiles(File componentsFile) { if ((componentsFile != null) && (!componentsFile.delete())) { componentsFile.deleteOnExit(); }//from w ww . j av a2 s . c om }
From source file:com.heliosdecompiler.helios.tasks.AddFilesTask.java
private void handleFile(File file) { System.out.println("Handling file " + file); String extension = FilenameUtils.getExtension(file.getName()).toLowerCase(); File fileToLoad = file;//from w ww . j a v a2s . c o m if (extension.equals("apk")) { try { if (Settings.APKTOOL.get().asBoolean()) { File decodedResources = File.createTempFile("apktoolout", ".apk"); decodedResources.deleteOnExit(); APKTool.decodeResources(file, decodedResources); fileToLoad = decodedResources; } } catch (final Exception e) { ExceptionHandler.handle(e); } } if (extension.equals("apk") || extension.equals("dex")) { try { if (Settings.APK_CONVERSION.get().asString().equals(Converter.ENJARIFY.getId())) { File transformedResources = File.createTempFile("enjarifyout", ".jar"); transformedResources.deleteOnExit(); Converter.ENJARIFY.convert(fileToLoad, transformedResources); fileToLoad = transformedResources; } else if (Settings.APK_CONVERSION.get().asString().equals(Converter.DEX2JAR.getId())) { File transformedResources = File.createTempFile("dex2jarout", ".jar"); transformedResources.deleteOnExit(); Converter.DEX2JAR.convert(fileToLoad, transformedResources); fileToLoad = transformedResources; } } catch (final Exception e) { ExceptionHandler.handle(e); } } try { Helios.loadFile(fileToLoad); } catch (final Exception e) { ExceptionHandler.handle(e); } }
From source file:it.tidalwave.northernwind.core.impl.util.CachedURIResolver.java
/******************************************************************************************************************* * ******************************************************************************************************************/ private void cacheDocument(final @Nonnull File cachedFile, final @Nonnull String href) throws IOException { log.debug(">>>> caching external document to {}", cachedFile); final File tempFile = File.createTempFile("temp", ".txt", new File(cacheFolderPath)); tempFile.deleteOnExit(); log.debug(">>>> waiting for lock..."); try (final FileChannel channel = new RandomAccessFile(tempFile, "rw").getChannel(); final FileLock lock = channel.lock()) { log.debug(">>>> got lock: {}", lock); FileUtils.copyURLToFile(new URL(href), tempFile, connectionTimeout, readTimeout); rename(tempFile, cachedFile);// w w w . j av a2 s . co m } catch (IOException e) { if (cachedFile.exists()) { log.warn("Error while retrieving document from {}: {} - using previously cached file", href, e.toString()); } else { throw e; } } log.debug(">>>> done"); }
From source file:net.lmxm.ute.utils.FileSystemUtilsTest.java
/** * Test create directory.//from www. j a va2 s. co m * * @throws IOException Signals that an I/O exception has occurred. */ @Test public void testCreateDirectory() throws IOException { final File directory = File.createTempFile("UTE", ".TEST"); directory.deleteOnExit(); assertTrue(directory.exists()); final File newDirectory1 = FILE_SYSTEM_UTILS.createDirectory(directory.getAbsolutePath()); assertNotNull(newDirectory1); assertTrue(newDirectory1.exists()); assertEquals(directory, newDirectory1); final File newDirectory2 = FILE_SYSTEM_UTILS.createDirectory(directory.getAbsolutePath()); assertNotNull(newDirectory2); assertTrue(newDirectory2.exists()); assertEquals(newDirectory1, newDirectory2); final File newDirectory3 = FILE_SYSTEM_UTILS.createDirectory(TMP_DIR + File.separator + "TESTFILE.TEST"); assertNotNull(newDirectory3); assertTrue(newDirectory3.exists()); assertTrue(newDirectory3.delete()); }