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.centeractive.ws.builder.DefinitionSaveTest.java

public static File createTempFolder(String name) throws IOException {
    File tempFolder = File.createTempFile(name, Long.toString(System.nanoTime()));
    if (!tempFolder.delete()) {
        throw new RuntimeException("cannot delete tmp file");
    }//from   w  w w . ja va  2s. com
    if (!tempFolder.mkdir()) {
        throw new RuntimeException("cannot create tmp folder");
    }
    return tempFolder;
}

From source file:com.joyent.manta.config.ConfigContextTest.java

public void canValidateContextWithClientEncryptionDisabled() throws IOException {
    File mantaAuthPrivateKey = File.createTempFile("manta-key", "");
    FileUtils.forceDeleteOnExit(mantaAuthPrivateKey);
    FileUtils.write(mantaAuthPrivateKey, UnitTestConstants.PRIVATE_KEY, StandardCharsets.US_ASCII);

    StandardConfigContext config = new StandardConfigContext();
    config.setMantaURL(DefaultsConfigContext.DEFAULT_MANTA_URL);
    config.setMantaUser("username");
    config.setMantaKeyId(UnitTestConstants.FINGERPRINT);
    config.setMantaKeyPath(mantaAuthPrivateKey.getAbsolutePath());
    config.setClientEncryptionEnabled(false);

    ConfigContext.validate(config);/*from w  w  w  .j  a  v  a 2  s  .  com*/
}

From source file:com.autentia.wuija.reports.JasperReportsHelper.java

public static File writeExportedReportToFile(String fileName, Format format,
        final ByteArrayOutputStream output) {

    File file;/*from   ww w.  j ava  2  s  .c  om*/
    FileOutputStream fos = null;
    try {
        file = File.createTempFile(fileName, "." + format.toString().toLowerCase());
        fos = new FileOutputStream(file);
        fos.write(output.toByteArray());
        fos.flush();

    } catch (IOException e) {
        final String msg = "Exception writing the report generated.";
        log.error(msg, e);
        throw new JRRuntimeException(msg, e);

    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                log.warn("Cannot close file of generated report", e);
            }
        }
    }
    return file;
}

From source file:de.tarent.maven.plugins.pkg.generator.SpecFileGeneratorTest.java

@Before
public void setUp() throws IOException {
    specgenerator = new SpecFileGenerator();
    spec = File.createTempFile("SPECFileGeneratorTest", "out");
    dummytestscript = File.createTempFile("dummytestscript", null);
}

From source file:de.bund.bfr.fskml.RScriptTest.java

public void testScript() throws IOException {

    // Creates temporary file. Fails the test if an error occurs.
    File f = File.createTempFile("temp", "");
    f.deleteOnExit();//from   ww w  . j  av a 2 s . c o m

    String origScript = "# This is a comment line: It should not appear in the simplified version\n"
            + "library(triangle)\n" // Test library command without quotes
            + "library(\"dplyr\")\n" // Test library command with double quotes
            + "library('devtools')\n" // Test library command with simple quotes
            + "library(foreign) # (a)\n" // Test library command followed by a comment (with parentheses)
            + "source('other.R')\n" // Source command with simple quotes
            + "source(\"other2.R\")\n" // Source command with double quotes
            + "hist(result, breaks=50, main=\"PREVALENCE OF PARENT FLOCKS\")\n";
    FileUtils.writeStringToFile(f, origScript, "UTF-8");
    RScript rScript = new RScript(f);

    assertEquals(origScript, rScript.getScript());
    assertEquals(Arrays.asList("triangle", "dplyr", "devtools", "foreign"), rScript.getLibraries());
    assertEquals(Arrays.asList("other.R", "other2.R"), rScript.getSources());
}

From source file:cleaner.ExternalHtmlCleaner.java

@Override
public String CleanText(String html, String encoding) {
    try {// www. jav  a 2  s  . c  o m
        File tempIn = File.createTempFile("ExternalParserInput", ".tmp");
        String tempInName = tempIn.getCanonicalPath();
        File tempOut = File.createTempFile("ExternalParserOutput", ".tmp");
        String tempOutName = tempOut.getCanonicalPath();

        FileUtils.writeStringToFile(tempIn, html, encoding);

        String cmd = extScript + " " + tempInName + " " + tempOutName;
        System.out.println("Executing: " + cmd);
        Process proc = Runtime.getRuntime().exec(cmd);

        if (proc == null) {
            System.err.println("Cannot execute command: " + extScript);
            return null;
        }

        StringWriter err = new StringWriter();
        IOUtils.copy(proc.getErrorStream(), err, encoding);

        String ErrStr = err.toString();

        if (!ErrStr.isEmpty()) {
            System.err.println("External script " + extScript + " returned errors:");
            System.err.println(ErrStr);

            throw new Exception("External script " + extScript + " returned errors");
        }

        String out = FileUtils.readFileToString(tempOut);

        tempIn.delete();
        tempOut.delete();

        return LeoCleanerUtil.CollapseSpaces(out);
    } catch (Exception e) {
        System.err.println("Failed to run the script " + extScript + " Error: " + e);
        System.exit(1);
    }
    return null;
}

From source file:com.milaboratory.core.io.util.TestUtil.java

public static File createRandomFile(long seed, int avLinesCount) throws IOException {
    File temp = File.createTempFile("temp" + seed, "tmp");
    temp.deleteOnExit();//from www  .  j a va  2s  . com
    FileOutputStream output = new FileOutputStream(temp);
    Well1024a random = new Well1024a(seed);

    int numberOfLines = avLinesCount + random.nextInt(10);
    for (int i = 0; i < numberOfLines; ++i) {
        StringBuilder sb = new StringBuilder();
        for (int j = (1 + random.nextInt(100)); j >= 0; --j)
            sb.append(random.nextInt(100));
        String string = sb.toString();
        byte[] bytes = string.getBytes();
        output.write(bytes);
        output.write('\n');
    }
    output.close();
    return temp;
}

From source file:com.tc.websocket.runners.TempFileMonitor.java

@Override
public void run() {

    if (TaskRunner.getInstance().isClosing()) {
        return;/*from  w w  w  .  j a  v a  2 s.c om*/
    }

    try {
        File temp = File.createTempFile("temp", "temp");
        String absolutePath = temp.getAbsolutePath();
        String tempFilePath = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator));

        System.out.println("cleaning out directory " + tempFilePath);

        File tempdir = new File(tempFilePath);
        File[] files = tempdir.listFiles();

        temp.delete();//cleanup

        if (files != null) {
            for (File file : files) {
                String name = file.getName();
                if (file.exists() && name.startsWith("eo") && name.endsWith("tm")) {
                    //calculate the age
                    Date lastmod = new Date(file.lastModified());
                    long minutes = DateUtils.getTimeDiffMin(lastmod, new Date());
                    if (minutes >= 5) {
                        FileUtils.deleteQuietly(file);
                    }
                }
            }
        }
    } catch (Exception e) {
        LOG.log(Level.SEVERE, null, e);
    }
}

From source file:com.googlecode.fannj.FannTrainerTest.java

@Test
public void testTrainingDefault() throws IOException {

    File temp = File.createTempFile("fannj_", ".tmp");
    temp.deleteOnExit();//from  www.j  a va2s.c o m
    IOUtils.copy(this.getClass().getResourceAsStream("xor.data"), new FileOutputStream(temp));

    List<Layer> layers = new ArrayList<Layer>();
    layers.add(Layer.create(2));
    layers.add(Layer.create(3, ActivationFunction.FANN_SIGMOID_SYMMETRIC));
    layers.add(Layer.create(1, ActivationFunction.FANN_SIGMOID_SYMMETRIC));

    Fann fann = new Fann(layers);
    Trainer trainer = new Trainer(fann);

    float desiredError = .001f;
    float mse = trainer.train(temp.getPath(), 500000, 1000, desiredError);
    assertTrue("" + mse, mse <= desiredError);
}

From source file:com.netflix.niws.client.http.SecureAcceptAllGetTest.java

@BeforeClass
public static void init() throws Exception {

    // setup server 1, will use first keystore/truststore with client auth
    TEST_PORT = new Random().nextInt(1000) + 4000;
    TEST_SERVICE_URI = "https://127.0.0.1:" + TEST_PORT + "/";

    // jks format
    byte[] sampleTruststore1 = Base64.decode(SecureGetTest.TEST_TS1);
    byte[] sampleKeystore1 = Base64.decode(SecureGetTest.TEST_KS1);

    TEST_FILE_KS = File.createTempFile("SecureAcceptAllGetTest", ".keystore");
    TEST_FILE_TS = File.createTempFile("SecureAcceptAllGetTest", ".truststore");

    FileOutputStream keystoreFileOut = new FileOutputStream(TEST_FILE_KS);
    try {//from   w w  w .  j a v  a 2  s .c om
        keystoreFileOut.write(sampleKeystore1);
    } finally {
        keystoreFileOut.close();
    }

    FileOutputStream truststoreFileOut = new FileOutputStream(TEST_FILE_TS);
    try {
        truststoreFileOut.write(sampleTruststore1);
    } finally {
        truststoreFileOut.close();
    }

    try {
        TEST_SERVER = new SimpleSSLTestServer(TEST_FILE_TS, SecureGetTest.PASSWORD, TEST_FILE_KS,
                SecureGetTest.PASSWORD, TEST_PORT, false);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

}