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:com.greenpepper.server.license.LicenceGenerator.java

private static void buildOpenSource() throws Exception {
    File file = File.createTempFile("opensource", ".lic");
    License license = License.openSource("My Open Source Project", _2006, _2006);
    LicenseManager lm = new LicenseManager(getLicenseParam());
    lm.store(license, file);//www . j a v a2  s  .com
    if (deleteFiles)
        file.deleteOnExit();
    System.out.println("# Open source");
    System.out.println(new String(Base64.encodeBase64(FileUtils.readFileToByteArray(file))));
    System.out.println("");
}

From source file:com.github.abhinavmishra14.aws.util.AWSUtil.java

/**
 * Creates the temp file from stream./*from  w w  w  .  ja va2 s . com*/
 *
 * @param inputStream the in stream
 * @return the file
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static File createTempFileFromStream(final InputStream inputStream) throws IOException {
    final File tempFile = File.createTempFile(AWSUtilConstants.TEMP_FILE_PREFIX,
            AWSUtilConstants.TEMP_FILE_SUFFIX);
    FileUtils.copyInputStreamToFile(inputStream, tempFile);
    return tempFile;
}

From source file:Main.java

/** Carga una librería nativa del sistema.
 * @param path Ruta a la libreria de sistema.
 * @throws IOException Si ocurre algún problema durante la carga */
public static void loadNativeLibrary(final String path) throws IOException {
    if (path == null) {
        LOGGER.warning("No se puede cargar una biblioteca nula"); //$NON-NLS-1$
        return;/* w  w  w.java  2 s .com*/
    }
    final int pos = path.lastIndexOf('.');
    final File file = new File(path);
    final File tempLibrary = File.createTempFile(
            pos < 1 ? file.getName() : file.getName().substring(0, file.getName().indexOf('.')),
            pos < 1 || pos == path.length() - 1 ? null : path.substring(pos));

    // Copiamos el fichero
    copyFile(file, tempLibrary);

    // Pedimos borrar los temporales cuando se cierre la JVM
    if (tempLibrary != null) {
        tempLibrary.deleteOnExit();
    }

    LOGGER.info("Cargamos " + (tempLibrary == null ? path : tempLibrary.getAbsolutePath())); //$NON-NLS-1$
    System.load(tempLibrary != null ? tempLibrary.getAbsolutePath() : path);

}

From source file:com.liferay.portal.search.elasticsearch.internal.util.ResourceUtil.java

public static File getResourceAsTempFile(Class<?> clazz, String name) throws IOException {

    int index = name.lastIndexOf(CharPool.PERIOD);

    File file = File.createTempFile(name.substring(0, index), name.substring(index));

    file.deleteOnExit();//from w w  w. j  a v a2s.co m

    try (InputStream inputStream = clazz.getResourceAsStream(name)) {
        FileUtils.copyInputStreamToFile(inputStream, file);
    }

    return file;
}

From source file:com.technofovea.packbsp.PackbspUtil.java

/**
 * Efficiently copies a given file, returning a temporary copy which will be
 * removed when execution finishes./*  w w  w.  j  av  a  2  s.  c  om*/
 * 
 * @param source Source file to copy.
 * @return The copied file.
 * @throws IOException
 */
public static final File createTempCopy(File source) throws IOException {
    String ext = FilenameUtils.getExtension(source.getName());
    File dest = File.createTempFile("packbsp_temp_", "." + ext);
    dest.deleteOnExit();
    FileInputStream fis = new FileInputStream(source);
    FileOutputStream fos = new FileOutputStream(dest);
    IOUtils.copy(fis, fos);
    fis.close();
    fos.close();
    return dest;
}

From source file:eu.scape_project.archiventory.utils.IOUtils.java

/**
 * Copy input stream to temporary file/* w  w  w .  jav a 2s .c o  m*/
 *
 * @param is Input sream
 * @param prefix Prefix of temporary file
 * @param ext Extension of temporary file
 * @return Temporary file
 */
public static File copyInputStreamToTempFile(InputStream is, String prefix, String ext) {
    FileOutputStream fos = null;
    File tmpFile = null;
    try {
        tmpFile = File.createTempFile(prefix, ext);
        fos = new FileOutputStream(tmpFile);
        org.apache.commons.io.IOUtils.copy(is, fos);
        fos.flush();
    } catch (FileNotFoundException ex) {
        logger.error("Temporary file not available.", ex);
    } catch (IOException ex) {
        logger.error("I/O Error occured.", ex);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException _) {
                // ignore
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException _) {
                // ignore
            }
        }
        return tmpFile;
    }
}

From source file:com.netflix.config.DynamicFileConfigurationTest.java

static File createConfigFile(String prefix) throws Exception {
    configFile = File.createTempFile(prefix, ".properties");
    BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(configFile), "UTF-8"));
    writer.write("dprops1=123456789");
    writer.newLine();//from  w w w . j  a  va 2 s  .c  om
    writer.write("dprops2=79.98");
    writer.newLine();
    writer.close();
    System.err.println(configFile.getPath() + " created");
    return configFile;
}

From source file:dataflow.examples.DockerClientExample.java

static private String createLocalTempDir() {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmss");
    Date date = new Date();
    String timestamp = formatter.format(date);

    // TODO(jlewi): Is there a java function we could use?
    File temp = null;//from   w w  w.ja va  2 s .  c o  m
    try {
        temp = File.createTempFile("temp-" + timestamp + "-", "");
    } catch (IOException exception) {
        logger.error("Could not create temporary file.", exception);
        System.exit(-1);
    }

    if (!(temp.delete())) {
        throw new RuntimeException("Could not delete temp file: " + temp.getAbsolutePath());
    }

    if (!(temp.mkdir())) {
        throw new RuntimeException("Could not create temp directory: " + temp.getAbsolutePath());
    }
    return temp.getPath();
}

From source file:com.amazonaws.util.FileUtils.java

/**
 * Returns a reference to the file created with the given file name in the
 * System's temporary directory./*from  www.ja  v a2 s . c  o  m*/
 * 
 * @param fileName
 * @return a reference to the file
 * @throws IOException
 */
public static File createTempFileForTesting(String fileName) throws IOException {
    return File.createTempFile(String.valueOf(System.currentTimeMillis()), fileName);

}

From source file:Main.java

/**
 * Create a temporary file that will be deleted after a specified number of seconds.
 * The file will be deleted regardless of whether it is still used or not,
 * so be sure to specify a sufficiently large value.
 *
 * @param lifetimeInSeconds the number of seconds after which the file will be
 *                          deleted -- e.g., 3600 means that the file will be deleted one hour after creation.
 * @return the File that was created./* w  ww.  j  a  v a 2  s  .  com*/
 * @throws IOException
 */
public static File createSelfDeletingTempFile(int lifetimeInSeconds) throws IOException {
    final File f = File.createTempFile("mary", "temp");
    maintenanceTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            f.delete();
        }
    }, lifetimeInSeconds * 1000l);
    return f;
}