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:com.thoughtworks.go.helpers.FileSystemUtils.java

public static File createFile(String filename) throws IOException {
    File file = new File(ROOT + File.separator + filename);
    file.createNewFile();//from   w w w .  ja  v  a2 s .co  m
    file.deleteOnExit();
    return file;
}

From source file:com.pentaho.repository.importexport.PDIImportUtil.java

/**
 * @return instance of {@link Document}, if xml is loaded successfully null in case any error occurred during loading
 *///ww  w.j a  v  a  2 s .  co m
public static Document loadXMLFrom(InputStream is) {
    DocumentBuilderFactory factory;
    try {
        factory = XMLParserFactoryProducer.createSecureDocBuilderFactory();
    } catch (ParserConfigurationException e) {
        log.logError(e.getLocalizedMessage());
        factory = DocumentBuilderFactory.newInstance();
    }
    DocumentBuilder builder = null;
    Document doc = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        // ignore
    }
    try {
        File file = File.createTempFile("tempFile", "temp");
        file.deleteOnExit();
        FileOutputStream fous = new FileOutputStream(file);
        IOUtils.copy(is, fous);
        fous.flush();
        fous.close();
        doc = builder.parse(file);
    } catch (IOException | SAXException e) {
        log.logError(e.getLocalizedMessage());
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // nothing to do here
        }
    }
    return doc;
}

From source file:es.uvigo.ei.sing.laimages.core.entities.datasets.VerticalElementDataTest.java

private final static File exportElementToTmpCSVFile(ElementData data, FileFormat format) throws IOException {
    File exportFile = File.createTempFile(data.getName(), ".csv");
    exportFile.deleteOnExit();

    data.toCSV(exportFile, new CSVFormat(format));

    return exportFile;
}

From source file:de.bund.bfr.pmfml.file.CombineArchiveUtil.java

static ArchiveEntry writeModel(CombineArchive archive, SBMLDocument doc, String docName, URI modelUri)
        throws IOException, SBMLException, XMLStreamException {
    // Creates temporary file for the model
    final File tmpFile = File.createTempFile("tmp", ".sbml");
    tmpFile.deleteOnExit();
    // Writes model to tmpFile and adds it to the file
    WRITER.write(doc, tmpFile);//from   w w w . ja v  a  2 s  .  c  o  m
    return archive.addEntry(tmpFile, docName, modelUri);
}

From source file:com.openshift.client.utils.TarFileTestUtils.java

/**
 * Replaces the given file(-name), that might exist anywhere nested in the
 * given archive, by a new entry with the given content. The replacement is
 * faked by adding a new entry into the archive which will overwrite the
 * existing (older one) on extraction./*from w  w  w . j  a  v a 2s. c o m*/
 * 
 * @param name
 *            the name of the file to replace (no path required)
 * @param newContent
 *            the content of the replacement file
 * @param in
 * @return
 * @throws IOException
 * @throws ArchiveException
 * @throws CompressorException
 */
public static File fakeReplaceFile(String name, String newContent, InputStream in) throws IOException {
    Assert.notNull(name);
    Assert.notNull(in);

    File newArchive = FileUtils.createRandomTempFile(".tar.gz");
    newArchive.deleteOnExit();

    TarArchiveOutputStream newArchiveOut = new TarArchiveOutputStream(
            new GZIPOutputStream(new FileOutputStream(newArchive)));
    newArchiveOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in));
    String pathToReplace = null;
    try {
        // copy the existing entries
        for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) {
            if (nextEntry.getName().endsWith(name)) {
                pathToReplace = nextEntry.getName();
            }
            newArchiveOut.putArchiveEntry(nextEntry);
            IOUtils.copy(archiveIn, newArchiveOut);
            newArchiveOut.closeArchiveEntry();
        }

        if (pathToReplace == null) {
            throw new IllegalStateException("Could not find file " + name + " in the given archive.");
        }
        TarArchiveEntry newEntry = new TarArchiveEntry(pathToReplace);
        newEntry.setSize(newContent.length());
        newArchiveOut.putArchiveEntry(newEntry);
        IOUtils.copy(new ByteArrayInputStream(newContent.getBytes()), newArchiveOut);
        newArchiveOut.closeArchiveEntry();

        return newArchive;
    } finally {
        newArchiveOut.finish();
        newArchiveOut.flush();
        StreamUtils.close(archiveIn);
        StreamUtils.close(newArchiveOut);
    }
}

From source file:gemlite.core.internal.support.system.WorkPathHelper.java

private static boolean lock(File directory) {
    String name = "node.lock";
    File lockfile = new File(directory, name);
    lockfile.deleteOnExit();
    try {// w  ww  . java 2s .  com
        FileChannel fc = (FileChannel) Channels.newChannel(new FileOutputStream(lockfile));
        lock = fc.tryLock();
        if (lock != null) {
            return true;
        }
    } catch (IOException x) {
        System.err.println(x.toString());
    } catch (OverlappingFileLockException e) {
        System.err.println(e.toString());
    }
    return false;
}

From source file:com.izforge.izpack.integration.UninstallHelper.java

/**
 * Helper to make a temporary copy of a uninstaller jar.
 *
 * @param uninstallJar the jar to copy/*from   ww  w.j a  v  a  2  s.c  o m*/
 * @return the copy
 * @throws IOException for any I/O error
 */
private static File copy(File uninstallJar) throws IOException {
    File copy = File.createTempFile("uninstaller", ".jar");
    copy.deleteOnExit();
    FileUtils.copyFile(uninstallJar, copy);
    return copy;
}

From source file:com.thoughtworks.go.util.TestFileUtil.java

public static File writeStringToTempFileInFolder(String directoryName, String fileName, String contents)
        throws Exception {
    File folderToCreate = tempFiles.createUniqueFolder(directoryName);
    File fileToCreate = new File(folderToCreate, fileName);
    folderToCreate.mkdirs();/*w w w .  j  av  a  2 s .  c  o m*/
    FileUtils.writeStringToFile(fileToCreate, contents, UTF_8);
    fileToCreate.deleteOnExit();
    folderToCreate.deleteOnExit();
    return fileToCreate;
}

From source file:com.loy.MicroServiceConsole.java

@SuppressWarnings("rawtypes")
static void init() {

    ClassPathResource classPathResource = new ClassPathResource("application.yml");
    Yaml yaml = new Yaml();
    Map result = null;//from w w w  .j  av  a 2s  .c  o m
    try {
        result = (Map) yaml.load(classPathResource.getInputStream());
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    String platformStr = result.get("platform").toString();
    String version = result.get("version").toString();
    String jvmOption = result.get("jvmOption").toString();
    @SuppressWarnings("unchecked")
    List<String> projects = (List<String>) result.get("projects");
    platform = Platform.valueOf(platformStr);

    final Runtime runtime = Runtime.getRuntime();
    File pidsForder = new File("./pids");
    if (!pidsForder.exists()) {
        pidsForder.mkdir();
    } else {
        File[] files = pidsForder.listFiles();
        if (files != null) {
            for (File f : files) {
                f.deleteOnExit();
                String pidStr = f.getName();
                try {
                    Long pid = new Long(pidStr);
                    if (Processes.isProcessRunning(platform, pid)) {
                        if (Platform.Windows == platform) {
                            Processes.tryKillProcess(null, platform, new NullProcessor(), pid);
                        } else {
                            Processes.killProcess(null, platform, new NullProcessor(), pid);
                        }

                    }
                } catch (Exception ex) {
                }
            }
        }
    }

    File currentForder = new File("");
    String root = currentForder.getAbsolutePath();
    root = root.replace(File.separator + "build" + File.separator + "libs", "");
    root = root.replace(File.separator + "e-example-ms-start", "");
    final String rootPath = root;
    new Thread(new Runnable() {
        @Override
        public void run() {
            int size = projects.size();
            int index = 1;
            for (String value : projects) {
                String path = value;
                String[] values = value.split("/");
                value = values[values.length - 1];
                value = rootPath + "/" + path + "/build/libs/" + value + "-" + version + ".jar";
                final String command = value;
                try {
                    Process process = runtime
                            .exec("java " + jvmOption + " -Dfile.encoding=UTF-8 -jar " + command);
                    Long pid = Processes.processId(process);
                    pids.add(pid);
                    File pidsFile = new File("./pids", pid.toString());
                    pidsFile.createNewFile();
                    new WriteLogThread(process.getInputStream()).start();

                } catch (IOException e) {
                    e.printStackTrace();
                }
                synchronized (lock) {
                    try {
                        if (index < size) {
                            lock.wait();
                        } else {
                            index++;
                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }).start();
}

From source file:com.sldeditor.tool.html.ExportHTML.java

/**
 * Writes an InputStream to a temporary file.
 *
 * @param in the in//from  w  w w .ja  v  a2  s.  c o m
 * @return the file
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static File stream2file(InputStream in) throws IOException {
    final File tempFile = File.createTempFile(PREFIX, SUFFIX);
    tempFile.deleteOnExit();
    try (FileOutputStream out = new FileOutputStream(tempFile)) {
        IOUtils.copy(in, out);
    }
    return tempFile;
}