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.opengamma.transport.TaxonomyGatheringFudgeMessageSenderTest.java

public void noTaxonomyFileAvailableYet() throws IOException {
    File tmpFile = File.createTempFile("TaxonomyGatheringFudgeMessageSenderTest_noTaxonomyFileAvailableYet",
            ".properties");
    FileUtils.forceDelete(tmpFile);//w  w  w . j  av  a 2 s . c o m
    FileUtils.forceDeleteOnExit(tmpFile);

    FudgeContext context = new FudgeContext();
    CollectingFudgeMessageReceiver collectingReceiver = new CollectingFudgeMessageReceiver();
    ByteArrayFudgeMessageReceiver fudgeReceiver = new ByteArrayFudgeMessageReceiver(collectingReceiver);
    DirectInvocationByteArrayMessageSender byteArraySender = new DirectInvocationByteArrayMessageSender(
            fudgeReceiver);
    ByteArrayFudgeMessageSender fudgeSender = new ByteArrayFudgeMessageSender(byteArraySender, context);
    TaxonomyGatheringFudgeMessageSender gatheringSender = new TaxonomyGatheringFudgeMessageSender(fudgeSender,
            tmpFile.getAbsolutePath());

    assertTrue(gatheringSender.getCurrentTaxonomy().isEmpty());
}

From source file:com.googlesource.gerrit.plugins.uploadvalidator.ValidatorTestCase.java

@Before
public void init() throws IOException {
    repoFolder = File.createTempFile("Git", "");
    repoFolder.delete();/*from w ww . ja  v  a2  s  .c o m*/
    repo = TestUtils.createNewRepository(repoFolder);
}

From source file:be.idamediafoundry.sofa.livecycle.maven.GenerateComponentXmlMojoTest.java

@Before
public void setUp() throws Exception {
    result = File.createTempFile("test", "xml");
    original = new File(this.getClass().getResource("/base/base-component.xml").getFile());

}

From source file:com.plotsquared.iserver.util.FileUtils.java

/**
 * Add files to a zip file//from  w w  w.  jav a 2  s  .c  o m
 *
 * @param zipFile Zip File
 * @param files   Files to add to the zip
 * @param delete  If the original files should be deleted
 * @throws Exception If anything goes wrong
 */
public static void addToZip(final File zipFile, final File[] files, final boolean delete) throws Exception {
    Assert.notNull(zipFile, files);

    if (!zipFile.exists()) {
        if (!zipFile.createNewFile()) {
            throw new RuntimeException("Couldn't create " + zipFile);
        }
    }

    final File temporary = File.createTempFile(zipFile.getName(), "");
    //noinspection ResultOfMethodCallIgnored
    temporary.delete();

    if (!zipFile.renameTo(temporary)) {
        throw new RuntimeException("Couldn't rename " + zipFile + " to " + temporary);
    }

    final byte[] buffer = new byte[1024 * 16]; // 16mb

    ZipInputStream zis = new ZipInputStream(new FileInputStream(temporary));
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry e = zis.getNextEntry();
    while (e != null) {
        String n = e.getName();

        boolean no = true;
        for (File f : files) {
            if (f.getName().equals(n)) {
                no = false;
                break;
            }
        }

        if (no) {
            zos.putNextEntry(new ZipEntry(n));
            int len;
            while ((len = zis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
        }
        e = zis.getNextEntry();
    }
    zis.close();
    for (File file : files) {
        InputStream in = new FileInputStream(file);
        zos.putNextEntry(new ZipEntry(file.getName()));

        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }

        zos.closeEntry();
        in.close();
    }
    zos.close();
    temporary.delete();

    if (delete) {
        for (File f : files) {
            f.delete();
        }
    }
}

From source file:com.linkedin.pinot.util.TestUtils.java

public static String getFileFromResourceUrl(@Nonnull URL resourceUrl) {
    // For maven cross package use case, we need to extract the resource from jar to a temporary directory.
    String resourceUrlStr = resourceUrl.toString();
    if (resourceUrlStr.contains("jar!")) {
        try {//from w  ww  . ja  v  a 2 s  .com
            String extension = resourceUrlStr.substring(resourceUrlStr.lastIndexOf('.'));
            File tempFile = File.createTempFile("pinot-test-temp", extension);
            String tempFilePath = tempFile.getAbsolutePath();
            LOGGER.info("Extracting from " + resourceUrlStr + " to " + tempFilePath);
            FileUtils.copyURLToFile(resourceUrl, tempFile);
            return tempFilePath;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        return resourceUrl.getFile();
    }
}

From source file:com.amazonaws.eclipse.core.accounts.profiles.SdkCredentialsFileContentMonitorTest.java

@Before
public void setup() throws IOException {
    targetFile = File.createTempFile("aws-eclipse-credentials-file-monitor-file", null);
}

From source file:de.tudarmstadt.ukp.dkpro.lexsemresource.core.util.FileUtils.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 v a 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("dkpro_stream", "tmp");
        f.deleteOnExit();
        os = new FileOutputStream(f);
        IOUtils.copy(is, os);
        return f;
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(is);
    }
}

From source file:io.zatarox.satellite.impl.BackgroundWrapperTest.java

@BeforeClass
public static void setBefore() throws Exception {
    file = File.createTempFile("archive", ".jar");
    file.deleteOnExit();// w  w w. j av a2 s.c o  m
    final FileOutputStream out = new FileOutputStream(file);
    ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP,
            out);
    ZipArchiveEntry entry = new ZipArchiveEntry("META-INF/MANIFEST.MF");
    archive.putArchiveEntry(entry);
    final Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().putValue("Background-Process-Class",
            FakeBackgroundProcessImpl.class.getName());
    manifest.write(archive);
    archive.closeArchiveEntry();
    archive.close();
}

From source file:com.enonic.cms.framework.util.HttpServletRangeUtilTest.java

public HttpServletRangeUtilTest() throws Exception {
    final URL url = getClass().getResource("input.dat");

    INPUT_FILE = File.createTempFile("range-test", "dat");

    FileUtils.writeStringToFile(INPUT_FILE, Resources.toString(url, Charsets.UTF_8));

    INPUT_FILE.deleteOnExit();//from  w w  w  . j ava2 s.co m
}

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

/**
 * Creates a file with the given name in the System's temporary directory.
 * Adds the data to the given file and returns the reference to the file.
 * /*w  w w. j av  a  2  s.c  om*/
 * @param fileName
 * @param data
 * @return reference to the file.
 * @throws IOException
 */
public static File createTempFileForTesting(String fileName, String data) throws IOException {
    return appendDataToTempFile(File.createTempFile(String.valueOf(System.currentTimeMillis()), fileName),
            data);

}