List of usage examples for java.io File createTempFile
public static File createTempFile(String prefix, String suffix, File directory) throws IOException
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
From source file:net.acesinc.data.json.generator.log.FileLogger.java
private void logEvent(String event) { try {//from w w w. j av a 2 s . c o m File f = File.createTempFile(filePrefix, fileExtension, outputDirectory); FileUtils.writeStringToFile(f, event, "UTF-8"); } catch (IOException ioe) { log.error("Unable to create temp file"); } }
From source file:eu.scape_project.pit.util.FileUtils.java
/** * Get temporary file/* w w w .j av a2 s . com*/ * @param name File name * @param suffix Suffix * @return Temp file folder */ public static File getTmpFile(final String name, final String suffix) { String suffixToUse = suffix == null ? ".tmp" : suffix; if (!suffixToUse.startsWith(".")) { suffixToUse = "." + suffix; } String nameToUse = name == null ? "tmp" : name; File input = null; try { File folder = new File(JAVA_TMP, FileUtils.TMP_DIR); if (!folder.exists()) { boolean mkdirs = folder.mkdirs(); checkCreation(folder, mkdirs); } input = File.createTempFile(nameToUse, suffixToUse, folder); input.deleteOnExit(); } catch (IOException e) { logger.error("An error occurred while creating the temporary file: \"" + name + suffix + "\""); } return input; }
From source file:com.tascape.qa.th.webui.test.SeleniumIdeTests.java
protected boolean runSeleniumIdeFirefox(File html, String browserURL) throws Exception { File tempDir = new File(html.getParentFile() + "/temp/"); tempDir.mkdir();/*from w ww . java 2 s .c o m*/ File suite = File.createTempFile("selenium-ide-temp-suite-", ".html", tempDir); suite.deleteOnExit(); String content = TEST_SUITE_TEMPLATE.replaceAll("XXXXXXXXXXXXXXXXXXXX", html.getName()); FileUtils.write(suite, content); LOG.debug("Temp test suite file {}", suite.getAbsolutePath()); File result = this.saveIntoFile("ide-result", "html", ""); this.captureScreens(2000); HTMLLauncher launcher = new HTMLLauncher(this.seleniumServer); String ff = "*firefox"; String bin = sysConfig.getProperty(Firefox.SYSPROP_FF_BINARY); if (bin != null) { ff += " " + bin; } String pf = launcher.runHTMLSuite(ff, browserURL, suite.toURI().toURL().toString(), result, 36000, true); suite.delete(); return "PASSED".equals(pf); }
From source file:com.geewhiz.pacify.utils.FileUtils.java
public static File createEmptyFileWithSamePermissions(File forFile, String filePrefix) { try {//from w w w .ja va2 s. c om File folder = forFile.getParentFile(); File tmp = File.createTempFile(filePrefix, ".tmp", folder); setPosixPermissions(getPosixPermissions(forFile), tmp); return tmp; } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.sisrni.managedbean.EditorImages.java
public void uploadListener(FileUploadEvent event) { FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); String fileName = FilenameUtils.getName(event.getFile().getFileName()); String fileNamePrefix = FilenameUtils.getBaseName(fileName) + "_"; String fileNameSuffix = "." + FilenameUtils.getExtension(fileName); File uploadFolder = new File("/var/webapp/uploads"); try {// w w w.j a va 2 s . c om File result = File.createTempFile(fileNamePrefix, fileNameSuffix, uploadFolder); FileOutputStream fileOutputStream = new FileOutputStream(result); byte[] buffer = new byte[1024]; int bulk; InputStream inputStream = event.getFile().getInputstream(); while (true) { bulk = inputStream.read(buffer); if (bulk < 0) { break; } fileOutputStream.write(buffer, 0, bulk); fileOutputStream.flush(); } fileOutputStream.close(); inputStream.close(); String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("editor_input"); setText(value + "<img src=\"/rais/uploads/" + result.getName() + "\" />"); RequestContext.getCurrentInstance().update("editor_input"); FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); FacesContext.getCurrentInstance().addMessage(null, msg); } catch (IOException e) { e.printStackTrace(); FacesMessage error = new FacesMessage("The files were not uploaded!"); FacesContext.getCurrentInstance().addMessage(null, error); } }
From source file:arena.utils.FileUtils.java
public static File writeArrayToTempFile(byte data[], File parentDir, String prefix, String suffix) throws IOException { parentDir.mkdirs();//from w ww . j a v a2 s . c o m File outFile = File.createTempFile(prefix, FileUtils.protectExtensions(suffix), parentDir); writeArrayToFile(data, outFile); return outFile; }
From source file:com.oculusinfo.binning.io.impl.ZipResourcePyramidSourceTest.java
@Test public void test() { ZipArchiveOutputStream zos = null;/*from w w w .java 2 s.co m*/ File archive; String filename = ""; try { archive = File.createTempFile("test.", ".zip", null); archive.deleteOnExit(); filename = archive.getAbsolutePath(); zos = new ZipArchiveOutputStream(archive); for (int z = 0; z < 3; z++) { for (int x = 0; x < Math.pow(2, z); x++) { for (int y = 0; y < Math.pow(2, z); y++) { ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/tiles/" + z + "/" + x + "/" + y + ".dummy"); zos.putArchiveEntry(entry); } } } ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/metadata.json"); zos.putArchiveEntry(entry); zos.closeArchiveEntry(); zos.close(); zos = null; } catch (IOException e) { fail(e.getMessage()); } try { ZipResourcePyramidSource src = new ZipResourcePyramidSource(filename, "dummy"); TileIndex tileDef = new TileIndex(0, 0, 0, 1, 1); InputStream is = src.getSourceTileStream("test", tileDef); Assert.assertTrue(is != null); tileDef = new TileIndex(2, 3, 3, 1, 1); is = src.getSourceTileStream("test", tileDef); Assert.assertTrue(is != null); is = src.getSourceMetaDataStream("test"); Assert.assertTrue(is != null); } catch (IOException e) { fail(e.getMessage()); } }
From source file:com.oculusinfo.binning.io.impl.ZipResourcePyramidStreamSourceTest.java
@Test public void test() { ZipArchiveOutputStream zos = null;// www .j a va2s . c o m File archive; String filename = ""; try { archive = File.createTempFile("test.", ".zip", null); archive.deleteOnExit(); filename = archive.getAbsolutePath(); zos = new ZipArchiveOutputStream(archive); for (int z = 0; z < 3; z++) { for (int x = 0; x < Math.pow(2, z); x++) { for (int y = 0; y < Math.pow(2, z); y++) { ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/tiles/" + z + "/" + x + "/" + y + ".dummy"); zos.putArchiveEntry(entry); } } } ZipArchiveEntry entry = new ZipArchiveEntry(getDummyFile(), "test/metadata.json"); zos.putArchiveEntry(entry); zos.closeArchiveEntry(); zos.close(); zos = null; } catch (IOException e) { fail(e.getMessage()); } try { ZipResourcePyramidStreamSource src = new ZipResourcePyramidStreamSource(filename, "dummy"); TileIndex tileDef = new TileIndex(0, 0, 0, 1, 1); InputStream is = src.getTileStream("test", tileDef); Assert.assertTrue(is != null); tileDef = new TileIndex(2, 3, 3, 1, 1); is = src.getTileStream("test", tileDef); Assert.assertTrue(is != null); is = src.getMetaDataStream("test"); Assert.assertTrue(is != null); } catch (IOException e) { fail(e.getMessage()); } }
From source file:io.takari.maven.plugins.compile.javac.CompilerJavacLauncher.java
@Override public int compile(Map<File, Resource<File>> sources) throws IOException { File options = File.createTempFile("javac-forked", ".options", buildDirectory); File output = File.createTempFile("javac-forked", ".output", buildDirectory); compile(options, output, sources);// w w w . ja v a 2 s .c om // don't delete temp files in case of an exception // they maybe useful to debug the problem options.delete(); output.delete(); return sources.size(); }
From source file:fr.itinerennes.bundler.tasks.framework.AbstractCountedCsvTask.java
@PostExec public void prependLineCount() throws IOException { LOGGER.debug("Inserting line count at file head: {}", lineCount); final File output = getOutputFile(); final File source = File.createTempFile("itr-", output.getName(), output.getParentFile()); source.delete();/*from www . j a v a2 s. co m*/ FileUtils.moveFile(output, source); InputStream from = null; BufferedWriter to = null; try { from = new FileInputStream(source); to = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output), CHARSET)); to.write(String.valueOf(lineCount)); to.newLine(); final LineIterator i = IOUtils.lineIterator(from, CHARSET.name()); while (i.hasNext()) { to.write(i.next()); to.newLine(); } } finally { IOUtils.closeQuietly(from); IOUtils.closeQuietly(to); FileUtils.deleteQuietly(source); } }