Example usage for java.io File deleteOnExit

List of usage examples for java.io File deleteOnExit

Introduction

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

Prototype

public void deleteOnExit() 

Source Link

Document

Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

Usage

From source file:io.github.blindio.prospero.core.browserdrivers.phantomjs.PhantomJSInstaller.java

static void installPhantomJS() {
    File tempArchive = null;
    try {/* w  ww. j a  va 2 s.c o m*/
        tempArchive = File.createTempFile(PHANTOMJS_FILE_PREFIX, ArchiveFormat.getFileExtention());
        FileUtils.copyURLToFile(new URL(PhantomJSArchiveFile.getDownloadURL()), tempArchive);
        tempArchive.deleteOnExit();
        UnArchiver unarchiver = ArchiveFormat.getUnArchiver();
        unarchiver.setSourceFile(tempArchive);
        File destDir = new File(getPhantomJSInstallDirPath());
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        unarchiver.setDestDirectory(destDir);
        unarchiver.extract();
    } catch (Exception e) {
        throw new ProsperoRuntimeAutomationException(e);
    }
}

From source file:org.dkpro.lab.Util.java

/**
 * Makes the given stream available as a file. The created file is temporary
 * and deleted upon normal termination of the JVM. Still the file should be
 * deleted as soon as possible if it is no longer required. In case the JVM
 * crashes the file would not be deleted. The source stream is closed by
 * this operation in all cases.//w w  w .  j  a va 2s .  c o  m
 *
 * @param is
 *            the source.
 * @return the file.
 * @throws IOException
 *             in case of read or write problems.
 */
public static File getStreamAsFile(final InputStream is) throws IOException {
    OutputStream os = null;
    try {
        final File f = File.createTempFile("lab_stream", "tmp");
        f.deleteOnExit();
        os = new FileOutputStream(f);
        shove(is, os);
        return f;
    } finally {
        close(os);
        close(is);
    }
}

From source file:org.jboss.as.test.integration.security.loginmodules.common.Utils.java

public static File createTempPropFile(Map<String, String> props) {
    try {/*w w  w . j a  v  a  2s  .  c  o  m*/
        File propsFile = File.createTempFile("props", ".properties");
        propsFile.deleteOnExit();
        Writer writer = new FileWriter(propsFile);
        for (Map.Entry<String, String> entry : props.entrySet()) {
            writer.write(entry.getKey() + "=" + entry.getValue() + "\n");
        }
        writer.close();
        return propsFile;
    } catch (IOException e) {
        throw new RuntimeException("Temporary file could not be created or writen to!", e);
    }
}

From source file:com.abiquo.appliancemanager.ApplianceManagerAsserts.java

protected static File createUploadTempFile() throws IOException {
    Random rnd = new Random(System.currentTimeMillis());
    final String fileName = String.valueOf(rnd.nextLong());
    File file = File.createTempFile(fileName, ".uploadTest");

    RandomAccessFile f = new RandomAccessFile(file, "rw");
    f.setLength(UPLOAD_FILE_SIZE_BYTES);

    file.deleteOnExit();

    return file;//  w w w .j a va 2s. c  om
}

From source file:be.i8c.sag.util.FileUtils.java

/**
 * Extract files from a zipped (and jar) file
 *
 * @param internalDir The directory you want to copy
 * @param zipFile The file that contains the content you want to copy
 * @param to The directory you want to copy to
 * @param deleteOnExit If true, delete the files once the application has closed.
 * @throws IOException When failed to write to a file
 * @throws FileNotFoundException If a file could not be found
 *///from www .ja v  a  2 s .c o  m
public static void extractDirectory(String internalDir, File zipFile, File to, boolean deleteOnExit)
        throws IOException {
    try (ZipInputStream zip = new ZipInputStream(new FileInputStream(zipFile))) {
        while (true) {
            ZipEntry zipEntry = zip.getNextEntry();
            if (zipEntry == null)
                break;

            if (zipEntry.getName().startsWith(internalDir) && !zipEntry.isDirectory()) {
                File f = createFile(new File(to, zipEntry.getName()
                        .replace((internalDir.endsWith("/") ? internalDir : internalDir + "/"), "")));
                if (deleteOnExit)
                    f.deleteOnExit();
                OutputStream bos = new FileOutputStream(f);
                try {
                    IOUtils.copy(zip, bos);
                } finally {
                    bos.flush();
                    bos.close();
                }
            }

            zip.closeEntry();
        }
    }
}

From source file:io.github.jeddict.jcode.util.FileUtil.java

public static FileObject getFileObject(InputStream in) throws IOException {
    final File tempFile = File.createTempFile("pom", "xml");
    tempFile.deleteOnExit();
    try (FileOutputStream out = new FileOutputStream(tempFile)) {
        org.openide.filesystems.FileUtil.copy(in, out);
    }/*ww  w . j  ava  2s .c o  m*/
    return org.openide.filesystems.FileUtil.toFileObject(tempFile);
}

From source file:com.flexive.shared.FxFileUtils.java

/**
 * Remove the given file//from   w w w  .  j  a  v a2 s .c  o m
 *
 * @param file file to remove
 */
public static void removeFile(File file) {
    if (file == null || !file.exists())
        return;
    if (!file.delete())
        file.deleteOnExit();
}

From source file:com.izforge.izpack.util.IoHelper.java

/**
 * Creates a temp file with delete on exit rule. The extension is extracted from the template if
 * possible, else the default extension is used. The contents of template will be copied into
 * the temporary file./*from  ww w . j  a  v a2 s .  c o m*/
 *
 * @param templateFile         file to copy from and define file extension
 * @param defaultExtension file extension if no is contained in template
 * @return newly created and filled temporary file
 * @throws IOException if an I/O error occurred
 */
public static File copyToTempFile(File templateFile, String defaultExtension) throws IOException {
    String path = templateFile.getCanonicalPath();
    int pos = path.lastIndexOf('.');
    String ext = path.substring(pos);
    if (ext.isEmpty()) {
        ext = defaultExtension;
    }
    File tmpFile = File.createTempFile("izpack_io", ext);
    tmpFile.deleteOnExit();
    FileUtils.copyFile(templateFile, tmpFile);
    return tmpFile;
}

From source file:com.ghgande.j2mod.modbus.utils.TestUtils.java

/**
 * This method will extract the appropriate Modbus master tool into the
 * temp folder so that it can be used later
 *
 * @return The temporary location of the Modbus master tool.
 * @throws Exception/*from w w w  . j  ava2  s .  c o  m*/
 */
public static File loadModPollTool() throws Exception {

    // Load the resource from the library

    String osName = System.getProperty("os.name");

    // Work out the correct name

    String exeName;
    if (osName.matches("(?is)windows.*")) {
        osName = "win32";
        exeName = "modpoll.exe";
    } else {
        osName = "linux";
        exeName = "modpoll";
    }

    // Copy the native modpoll library to a temporary directory in the build workspace to facilitate
    // execution on some platforms.
    File tmpDir = Files.createTempDirectory(Paths.get("."), "modpoll-").toFile();
    tmpDir.deleteOnExit();

    File nativeFile = new File(tmpDir, exeName);

    // Copy the library to the temporary folder

    InputStream in = null;
    String resourceName = String.format("/com/ghgande/j2mod/modbus/native/%s/%s", osName, exeName);

    try {
        in = SerialPort.class.getResourceAsStream(resourceName);
        if (in == null) {
            throw new Exception(String.format("Cannot find resource [%s]", resourceName));
        }
        pipeInputToOutputStream(in, nativeFile, false);
        nativeFile.deleteOnExit();

        // Set the correct privileges

        if (!nativeFile.setWritable(true, true)) {
            logger.warn("Cannot set modpoll native library to be writable");
        }
        if (!nativeFile.setReadable(true, false)) {
            logger.warn("Cannot set modpoll native library to be readable");
        }
        if (!nativeFile.setExecutable(true, false)) {
            logger.warn("Cannot set modpoll native library to be executable");
        }
    } catch (Exception e) {
        throw new Exception(
                String.format("Cannot locate modpoll native library [%s] - %s", exeName, e.getMessage()));
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                logger.error("Cannot close stream - {}", e.getMessage());
            }
        }
    }

    return nativeFile;
}

From source file:eu.scape_project.pit.util.FileUtils.java

/**
 * Get temporary file//w ww  . j a  va 2  s. c  om
 * @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;
}