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, File directory) throws IOException 

Source Link

Document

Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

Usage

From source file:com.adaptris.core.services.ScriptingServiceTest.java

private File writeScript(boolean working) throws IOException {
    FileWriter fw = null;/*from  w  w  w  .  j  a  va 2s  .  c  om*/
    File result = null;
    try {
        File dir = new File(PROPERTIES.getProperty(KEY_SCRIPTING_BASEDIR, "./build/scripting"));
        dir.mkdirs();
        result = File.createTempFile("junit", ".script", dir);
        fw = new FileWriter(result);
        fw.write(working ? SCRIPT : "This will fail");
    } finally {
        IOUtils.closeQuietly(fw);
    }
    return result;
}

From source file:com.indeed.lsmtree.core.TestImmutableBTreeIndex.java

@Override
public void setUp() throws Exception {
    tmpDir = File.createTempFile("tmp", "", new File("."));
    tmpDir.delete();/*from w  ww .  ja  va 2s  . c  o m*/
    tmpDir.mkdirs();
    String treeSizeStr = System.getProperty("lsmtree.test.size");
    treeSize = "large".equals(treeSizeStr) ? LARGE_TREE_SIZE : SMALL_TREE_SIZE;
}

From source file:com.khepry.utilities.GenericUtilities.java

public static void displayLogFile(String logFilePath, long logSleepMillis, String xsltFilePath,
        String xsldFilePath) {/*w  ww .  j  a  v a 2  s .  c  o m*/
    Logger.getLogger("").getHandlers()[0].close();
    // only display the log file
    // if the logSleepMillis property
    // is greater than zero milliseconds
    if (logSleepMillis > 0) {
        try {
            Thread.sleep(logSleepMillis);
            File logFile = new File(logFilePath);
            File xsltFile = new File(xsltFilePath);
            File xsldFile = new File(xsldFilePath);
            File tmpFile = File.createTempFile("tmpLogFile", ".xhtml", logFile.getParentFile());
            if (logFile.exists()) {
                if (xsltFile.exists() && xsldFile.exists()) {
                    String xslFilePath;
                    String xslFileName;
                    String dtdFilePath;
                    String dtdFileName;
                    try {
                        xslFileName = new File(logFilePath).getName().replace(".xhtml", ".xsl");
                        xslFilePath = logFile.getParentFile().toString().concat("/").concat(xslFileName);
                        FileUtils.copyFile(new File(xsltFilePath), new File(xslFilePath));
                        dtdFileName = new File(logFilePath).getName().replace(".xhtml", ".dtd");
                        dtdFilePath = logFile.getParentFile().toString().concat("/").concat(dtdFileName);
                        FileUtils.copyFile(new File(xsldFilePath), new File(dtdFilePath));
                    } catch (IOException ex) {
                        String message = Level.SEVERE.toString().concat(": ").concat(ex.getLocalizedMessage());
                        Logger.getLogger(GenericUtilities.class.getName()).log(Level.SEVERE, message, ex);
                        GenericUtilities.outputToSystemErr(message, logSleepMillis > 0);
                        return;
                    }
                    BufferedWriter bw = Files.newBufferedWriter(Paths.get(tmpFile.getAbsolutePath()),
                            Charset.defaultCharset(), StandardOpenOption.CREATE);
                    List<String> logLines = Files.readAllLines(Paths.get(logFilePath),
                            Charset.defaultCharset());
                    for (String line : logLines) {
                        if (line.startsWith("<!DOCTYPE log SYSTEM \"logger.dtd\">")) {
                            bw.write("<!DOCTYPE log SYSTEM \"" + dtdFileName + "\">\n");
                            bw.write("<?xml-stylesheet type=\"text/xsl\" href=\"" + xslFileName + "\"?>\n");
                        } else {
                            bw.write(line.concat("\n"));
                        }
                    }
                    bw.write("</log>\n");
                    bw.close();
                }
                // the following statement is commented out because it's not quite ready for prime-time yet
                // Files.write(Paths.get(logFilePath), transformLogViaXSLT(logFilePath, xsltFilePath).getBytes(), StandardOpenOption.CREATE);
                Desktop.getDesktop().open(tmpFile);
            } else {
                Logger.getLogger(GenericUtilities.class.getName()).log(Level.SEVERE, logFilePath,
                        new FileNotFoundException());
            }
        } catch (InterruptedException | IOException ex) {
            Logger.getLogger(GenericUtilities.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.android.email.core.internet.BinaryTempFileBody.java

public OutputStream getOutputStream() throws IOException {
    mFile = File.createTempFile("body", null, mTempDirectory);
    mFile.deleteOnExit();/*from   ww  w . j  a  va2s .c om*/
    return new FileOutputStream(mFile);
}

From source file:com.mail163.email.mail.internet.BinaryTempFileBody.java

public OutputStream getOutputStream() throws IOException {
    mFile = File.createTempFile("body", null, Email.getTempDirectory());
    mFile.deleteOnExit();//from   w  w  w. ja  va  2s . c o  m
    return new FileOutputStream(mFile);
}

From source file:at.uni_salzburg.cs.ckgroup.cscpp.engine.actions.Picture.java

@Override
protected boolean retrieveValue(ISensorProxy sprox) {
    InputStream inStream = sprox.getSensorValueAsStream(ISensorProxy.SENSOR_NAME_PHOTO);
    if (inStream == null) {
        return false;
    }//ww  w  .j a v  a  2s.  c o m

    try {
        File f = File.createTempFile("img", ".png", dataDir);
        setFilename(f.getName());
        FileOutputStream outStream = new FileOutputStream(f);
        FileUtils.copyStream(inStream, outStream, true);
        return true;
    } catch (IOException e) {
        LOG.error("Reading sensor " + ISensorProxy.SENSOR_NAME_PHOTO, e);
    }

    return false;
}

From source file:edu.ur.ir.file.DefaultTemporaryFileCreator.java

/**
 * Creates a temporary file and set the file to be deleted on exit.
 * /*w w w.  java 2s  .c om*/
 * @see edu.ur.ir.file.TemporaryFileCreator#createTemporaryFile(java.lang.String)
 */
public File createTemporaryFile(String extension) throws IOException {
    File tempDir = new File(temporaryDirectory);

    if (!tempDir.exists()) {
        FileUtils.forceMkdir(tempDir);
    }

    if (!tempDir.isDirectory()) {
        throw new RuntimeException("Temp directory is not a directory " + tempDir.getAbsolutePath());
    }

    if (!tempDir.canWrite()) {
        throw new RuntimeException("Could not write to directory " + tempDir.getAbsolutePath());
    }

    if (!tempDir.canRead()) {
        throw new RuntimeException("Could not read temp directory " + tempDir.getAbsolutePath());
    }
    File f = File.createTempFile("ur-temp", extension, tempDir);
    f.deleteOnExit();
    return f;
}

From source file:com.android.emailcommon.internet.BinaryTempFileBody.java

public OutputStream getOutputStream() throws IOException {
    mFile = File.createTempFile("body", null, TempDirectory.getTempDirectory());
    mFile.deleteOnExit();/*from  w w  w . ja  v a 2 s  .  c  om*/
    return new FileOutputStream(mFile);
}

From source file:it.openutils.mgnlaws.magnolia.datastore.CachedS3DataRecord.java

public CachedS3DataRecord(S3DataRecord record, File cacheDirectory) throws DataStoreException, IOException {
    super(record.getIdentifier());
    this.length = record.getLength();
    this.lastModified = record.getLastModified();

    fileToStream = File.createTempFile(this.getIdentifier().toString() + "-", null, cacheDirectory);
    fileToStreamAbsolutePath = fileToStream.getAbsolutePath();
    FileOutputStream fos = new FileOutputStream(fileToStream);
    try {//  w w w . j a v  a  2 s. c o  m
        IOUtils.copyLarge(record.getStream(), fos);
    } finally {
        IOUtils.closeQuietly(record.getStream());
        IOUtils.closeQuietly(fos);
    }
}

From source file:com.creactiviti.piper.core.taskhandler.io.Download.java

@Override
public Object handle(Task aTask) {
    try {//from w w w.j a  va2  s.c o m
        URL url = new URL(aTask.getRequiredString("url"));
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();

        if (connection.getResponseCode() / 100 == 2) {
            File downloadedFile = File.createTempFile("download-", "", null);
            int contentLength = connection.getContentLength();
            Consumer<Integer> progressConsumer = (p) -> eventPublisher.publishEvent(PiperEvent
                    .of(Events.TASK_PROGRESSED, "taskId", ((TaskExecution) aTask).getId(), "progress", p));

            try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
                    OutputStream os = new ProgressingOutputStream(new FileOutputStream(downloadedFile),
                            contentLength, progressConsumer)) {

                copy(in, os);
            }
            return downloadedFile.toString();
        }

        throw new IllegalStateException("Server returned: " + connection.getResponseCode());

    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}