Example usage for java.io File createTempFile

List of usage examples for java.io File createTempFile

Introduction

In this page you can find the example usage for java.io File createTempFile.

Prototype

public static File createTempFile(String prefix, String suffix) throws IOException 

Source Link

Document

Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

Usage

From source file:edu.uci.ics.hyracks.algebricks.core.algebra.prettyprint.PlanPlotter.java

public static void printLogicalPlan(ILogicalPlan plan) throws AlgebricksException {
    int indent = 5;
    StringBuilder out = new StringBuilder();
    int randomInt = 10000 + randomGenerator.nextInt(100);
    appendln(out, "digraph G {");
    for (Mutable<ILogicalOperator> root : plan.getRoots()) {
        printVisualizationGraph((AbstractLogicalOperator) root.getValue(), indent, out, "", randomInt);
    }/*  w  ww. j  a  v  a 2 s  .  co  m*/
    appendln(out, "\n}\n}");
    try {
        File file = File.createTempFile("logicalPlan", ".txt");
        FileUtils.writeStringToFile(file, out.toString());
        file.deleteOnExit();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.sf.nutchcontentexporter.WARCReaderTest.java

/**
 * Reads bz2 warc file//from   ww  w.  jav  a 2s. c o  m
 *
 * @param file warc file
 * @throws IOException
 */
public static void readBz2(String file) throws IOException {
    // decompress bz2 file to tmp file
    File tmpFile = File.createTempFile("tmp", ".warc");
    BZip2CompressorInputStream inputStream = new BZip2CompressorInputStream(new FileInputStream(file));

    IOUtils.copy(inputStream, new FileOutputStream(tmpFile));

    WARCReader reader = WARCReaderFactory.get(tmpFile);

    int counter = 0;
    for (ArchiveRecord record : reader) {
        System.out.println(record.getHeader().getHeaderFields());

        counter++;
    }

    FileUtils.forceDelete(tmpFile);

    System.out.println(counter);
}

From source file:eu.europa.esig.dss.pdf.DSSPDFUtils.java

/**
 * This method returns the temporary {@code File} with the provided contents.
 *
 * @param pdfData {@code InputStream} representing the contents of the returned {@code File}
 * @return {@code File} with the given contents
 * @throws DSSException in case of any {@code IOException}
 *///  w  w  w. java2 s.  co m
public static File getFileFromPdfData(final InputStream pdfData) throws DSSException {

    FileOutputStream fileOutputStream = null;
    try {

        final File file = File.createTempFile("sd-dss-", ".pdf");
        fileOutputStream = new FileOutputStream(file);
        IOUtils.copy(pdfData, fileOutputStream);
        return file;
    } catch (IOException e) {
        throw new DSSException("The process has no rights to write or to access 'java.io.tmpdir': "
                + System.getProperty("java.io.tmpdir"), e);
    } finally {
        IOUtils.closeQuietly(pdfData);
        IOUtils.closeQuietly(fileOutputStream);
    }
}

From source file:com.legstar.protobuf.cobol.ProtoCobolUtilsTest.java

public void testExtractNoPackageNameFromProtoFile() throws Exception {
    File protoFile = File.createTempFile(getName(), ".proto");
    protoFile.deleteOnExit();/*w  w w  .ja  v a2  s  . c o  m*/
    FileUtils.writeStringToFile(protoFile, "");
    ProtoFileJavaProperties javaProperties = ProtoCobolUtils.getJavaProperties(protoFile);
    assertNull(javaProperties.getJavaPackageName());
}

From source file:hu.bme.mit.trainbenchmark.benchmark.fourstore.driver.UnixUtils.java

public static String createTempFileFromResource(final String script, final String extension,
        final boolean executable) throws IOException {
    final InputStream scriptInputStream = UnixUtils.class.getResourceAsStream("/" + script);

    // create a temporary file
    final File scriptTempFile = File.createTempFile("unix-utils-", extension);
    scriptTempFile.deleteOnExit();/*from  ww w.j  a  v a  2 s  . c o m*/
    try (FileOutputStream out = new FileOutputStream(scriptTempFile)) {
        IOUtils.copy(scriptInputStream, out);
    }
    scriptTempFile.setExecutable(executable);

    final String command = scriptTempFile.getAbsolutePath();
    return command;
}

From source file:it.cnr.ilc.ilcioutils.IlcInputToFile.java

/**
 * Creates a file with the content read from string
 * @param string the String with the content is
 * @return a File with the content from the URL
 *///from  w w w . j a  v  a 2s  .c  o m
public static File createAndWriteTempFileFromString(String string) {
    File tempOutputFile = null;
    String encoding = "UTF-8";
    String message;
    try {
        tempOutputFile = File.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX);
        FileUtils.writeStringToFile(tempOutputFile, string, encoding);

    } catch (IOException e) {
        message = String.format("Error in accessing String %s %s", string, e.getMessage());
        Logger.getLogger(CLASS_NAME).log(Level.SEVERE, message);
    }
    return tempOutputFile;

}

From source file:org.wwscc.registration.attendance.Attendance.java

/**
 * Retrieve the attendance report from the main host
 * @param host the hostname to retrieve from
 * @throws IOException //from   w w  w .j  av  a 2s .  com
 * @throws URISyntaxException 
 * @throws UnsupportedEncodingException 
 */
public static void getAttendance(String host) throws IOException, URISyntaxException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpProtocolParams.setUserAgent(httpclient.getParams(), "Scorekeeper/2.0");

    MonitorProgressStream monitor = new MonitorProgressStream("Download Attendance");
    monitor.setProgress(1);
    monitor.setNote("Initialize");

    HttpPost request = new HttpPost(new URI("http", host, "/history/attendance", null));
    File temp = File.createTempFile("attendance", "tmp");
    monitor.setProgress(2);
    monitor.setNote("Connecting/Calcuation...");

    HttpEntity download = httpclient.execute(request).getEntity();
    monitor.setProgress(3);
    monitor.setNote("Downloading...");

    FileOutputStream todisk = new FileOutputStream(temp);
    monitor.setStream(todisk, download.getContentLength());
    download.writeTo(monitor);
    FileUtils.copyFile(temp, defaultfile);
}

From source file:com.sangupta.jerry.util.WebUtils.java

/**
 * Download the file at the given location URL and store it as a temporary
 * file on disk. The temporary file is set to be deleted at the exit of the
 * application./*from  w w  w. j  av a2 s.  c  o m*/
 * 
 * @param url
 *            absolute URL of the file
 * 
 * @return {@link File} handle of the temporary file that was written to
 *         disk if successful, <code>null</code> otherwise.
 * 
 * @throws IOException
 *             in case something fails
 */
public static File downloadToTempFile(String url) throws IOException {
    String extension = UriUtils.extractExtension(url);
    File tempFile = File.createTempFile("download", extension);
    tempFile.deleteOnExit();

    logger.debug("Downloading {} to {}", url, tempFile.getAbsolutePath());

    try {
        WebRequest.get(url).execute().writeToFile(tempFile);
        return tempFile;
    } catch (HttpResponseException e) {
        logger.error("HTTP response did not yield an OK status", e);
    } catch (IOException e) {
        logger.error("Unable to download url to temp file", e);
    }

    return null;
}

From source file:com.greenpepper.server.license.LicenceGenerator.java

private static void buildAcademic() throws Exception {
    File file = File.createTempFile("academic", ".lic");
    License license = License.academic("My School", _2006, _2006);
    LicenseManager lm = new LicenseManager(getLicenseParam());
    lm.store(license, file);/*from  w w  w .jav a 2  s  . c  o m*/
    if (deleteFiles)
        file.deleteOnExit();
    System.out.println("# Academic");
    System.out.println(new String(Base64.encodeBase64(FileUtils.readFileToByteArray(file))));
    System.out.println("");
}

From source file:com.ca.dvs.app.dvs_servlet.misc.FileUtil.java

/**
 * Create A File object from the content uploaded to the servlet
 * <p>/* w ww  .ja  va  2  s .  co m*/
 * @param uploadedInputStream the stream associated with a POSTed form containing a File argument
 * @param fileDetail the File specification for the uploadedInputStream
 * @return the File locally stored from the uploadedInputStream 
 * @throws Exception (IOException net.lingala.zip4j.exception.ZipException)
 */
public static File getUploadedFile(InputStream uploadedInputStream, FormDataContentDisposition fileDetail)
        throws Exception {

    File uploadedFile = null;
    File tmpDir = null;

    try {

        uploadedFile = File.createTempFile(fileDetail.getFileName(), null);

        FileUtils.copyInputStreamToFile(uploadedInputStream, uploadedFile);

        if (FileUtil.isZipFile(uploadedFile)) {

            tmpDir = Files.createTempDir();
            ZipFile zipFile = new ZipFile(uploadedFile);
            zipFile.extractAll(tmpDir.getAbsolutePath());

            uploadedFile.delete();

            uploadedFile = tmpDir;

        }

    } catch (IOException e) {

        String msg = String.format("Failed to store uploaded file - %s", e.getMessage());

        throw new Exception(msg, e.getCause());

    } catch (net.lingala.zip4j.exception.ZipException e) {

        String msg = String.format("Failed to store uploaded file - %s", e.getMessage());

        throw new Exception(msg, e.getCause());

    }

    return uploadedFile;
}