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:com.moz.fiji.mapreduce.TestingResources.java
/** * Reads a resource and writes it to a local temporary file. * * @param resourcePath Path of the resource to load. * @param directory Path to the directory to write the temporary file to. * @return A handle to the written temporary file. * @throws IOException on I/O error./*from w w w . ja v a 2 s . co m*/ */ public static File getResourceAsTempFile(final String resourcePath, final File directory) throws IOException { final File tempFile = File.createTempFile("TempResourceFile", "", directory); TestingResources.writeTextFile(tempFile, TestingResources.get(resourcePath)); return tempFile; }
From source file:com.android.email.mail.store.imap.ImapTempFileLiteral.java
ImapTempFileLiteral(FixedLengthInputStream stream) throws IOException { mSize = stream.getLength();/*from w w w. j a va 2s. com*/ mFile = File.createTempFile("imap", ".tmp", Email.getTempDirectory()); // Unfortunately, we can't really use deleteOnExit(), because temp filenames are random // so it'd simply cause a memory leak. // deleteOnExit() simply adds filenames to a static list and the list will never shrink. // mFile.deleteOnExit(); OutputStream out = new FileOutputStream(mFile); IOUtils.copy(stream, out); out.close(); }
From source file:com.mirth.connect.connectors.file.filesystems.test.FileConnectionTest.java
@Test public void testListFiles() throws Exception { ArrayList<String> testFileNames = new ArrayList<String>(); for (int i = 0; i < 10; i++) { File temp = File.createTempFile("ListFile", ".dat", someFolder); testFileNames.add(temp.getName()); }/*from w ww .j ava 2 s. co m*/ List<FileInfo> retFiles = fc.listFiles(someFolder.getAbsolutePath(), "ListFile.+", true, true); for (int i = 0; i < retFiles.size(); i++) { assertTrue(testFileNames.contains(retFiles.get(i).getName())); } }
From source file:com.janoz.usenet.support.LogUtil.java
public static InputStream dumpStreamAndReOffer(InputStream is) throws IOException { FileOutputStream fos = null;//from ww w . j a v a2 s . com try { ByteArrayOutputStream os = new ByteArrayOutputStream(); copyStream(is, os); byte[] data = os.toByteArray(); fos = new FileOutputStream(File.createTempFile("dump", ".bin", new File("."))); fos.write(data); return new ByteArrayInputStream(data); } finally { if (fos != null) { fos.close(); } } }
From source file:cn.suishen.email.mail.store.imap.ImapTempFileLiteral.java
ImapTempFileLiteral(FixedLengthInputStream stream) throws IOException { mSize = stream.getLength();/*from w w w . j a v a 2 s . c o m*/ mFile = File.createTempFile("imap", ".tmp", TempDirectory.getTempDirectory()); // Unfortunately, we can't really use deleteOnExit(), because temp filenames are random // so it'd simply cause a memory leak. // deleteOnExit() simply adds filenames to a static list and the list will never shrink. // mFile.deleteOnExit(); OutputStream out = new FileOutputStream(mFile); IOUtils.copy(stream, out); out.close(); }
From source file:io.druid.segment.writeout.TmpFileSegmentWriteOutMedium.java
@Override public WriteOutBytes makeWriteOutBytes() throws IOException { File file = File.createTempFile("filePeon", null, dir); FileChannel ch = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE); closer.register(file::delete);//from w w w.j av a 2 s . c o m closer.register(ch); return new FileWriteOutBytes(file, ch); }
From source file:fll.web.WebTestUtils.java
private static void checkForServerError(final Page page) throws IOException { final com.gargoylesoftware.htmlunit.WebResponse response = page.getWebResponse(); final int code = response.getStatusCode(); final boolean error; if (code >= 400) { error = true;//from w w w .ja v a 2s. c o m } else { error = false; } if (error) { final String responseMessage = response.getStatusMessage(); final String text = getPageSource(page); final File output = File.createTempFile("server-error", ".html", new File("screenshots")); final FileWriter writer = new FileWriter(output); writer.write(text); writer.close(); Assert.fail("Error loading page: " + page.getUrl() + " code: " + code + " message: " + responseMessage + " Contents of error page written to: " + output.getAbsolutePath()); } }
From source file:com.google.jenkins.plugins.credentials.oauth.P12ServiceAccountConfigTestUtil.java
public static String createTempP12KeyFile(KeyPair keyPair) throws IOException, OperatorCreationException, CertificateException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException { File tempP12Key = File.createTempFile("temp-key", ".p12", getTempFolder()); writeKeyToFile(keyPair, tempP12Key); return tempP12Key.getAbsolutePath(); }
From source file:com.bahmanm.karun.Utils.java
/** * Extracts a gzip'ed tar archive./*from w w w.j a va 2 s . co m*/ * * @param archivePath Path to archive * @param destDir Destination directory * @throws IOException */ public synchronized static void extractTarGz(String archivePath, File destDir) throws IOException, ArchiveException { // copy File tarGzFile = File.createTempFile("karuntargz", "", destDir); copyFile(archivePath, tarGzFile.getAbsolutePath()); // decompress File tarFile = File.createTempFile("karuntar", "", destDir); FileInputStream fin = new FileInputStream(tarGzFile); BufferedInputStream bin = new BufferedInputStream(fin); FileOutputStream fout = new FileOutputStream(tarFile); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(bin); final byte[] buffer = new byte[1024]; int n = 0; while (-1 != (n = gzIn.read(buffer))) { fout.write(buffer, 0, n); } bin.close(); fin.close(); gzIn.close(); fout.close(); // extract final InputStream is = new FileInputStream(tarFile); ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) ain.getNextEntry()) != null) { OutputStream out; if (entry.isDirectory()) { File f = new File(destDir, entry.getName()); f.mkdirs(); continue; } else out = new FileOutputStream(new File(destDir, entry.getName())); IOUtils.copy(ain, out); out.close(); } ain.close(); is.close(); }
From source file:com.izforge.izpack.panels.userinput.console.file.ConsoleFileFieldTest.java
/** * Sets up the test./* w w w . jav a 2s. c o m*/ * * @throws IOException for any error */ @Before public void aetUp() throws IOException { file = File.createTempFile("foo", "bar", FileUtils.getTempDirectory()); }