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:de.topobyte.largescalefileio.TestClosingFileInputStream.java

public void test(int n) throws IOException {
    files = new File[n];
    for (int i = 0; i < n; i++) {
        files[i] = File.createTempFile("closing-fis", ".dat");
    }//from   w w w .  java 2s .  co  m
    allFiles.addAll(Arrays.asList(files));

    ByteArrayGenerator generator = new ByteArrayGenerator();
    byte[][] bytes = new byte[n][];
    for (int i = 0; i < n; i++) {
        bytes[i] = generator.generateBytes(1024);
    }

    for (int i = 0; i < n; i++) {
        FileUtils.writeByteArrayToFile(files[i], bytes[i]);
    }

    ClosingFileInputStreamFactory factory = new SimpleClosingFileInputStreamFactory();

    InputStream[] inputs = new InputStream[n];
    for (int i = 0; i < n; i++) {
        inputs[i] = factory.create(files[i]);
    }

    byte[][] results = ReaderUtil.readInterleaved(inputs);

    for (int i = 0; i < n; i++) {
        Assert.assertArrayEquals(bytes[i], results[i]);
    }
}

From source file:net.sf.hajdbc.codec.crypto.CipherCodecFactoryTest.java

@Before
public void before() throws Exception {
    File file = File.createTempFile("ha-jdbc", "keystore");

    SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
    this.key = factory.generateSecret(new DESKeySpec(Base64.decodeBase64(KEY.getBytes())));
    KeyStore store = KeyStore.getInstance(CipherCodecFactory.Property.KEYSTORE_TYPE.defaultValue);
    store.load(null, null);//ww  w. j  av  a2 s .  c  o m
    store.setKeyEntry(CipherCodecFactory.Property.KEY_ALIAS.defaultValue, this.key, KEY_PASSWORD.toCharArray(),
            null);

    FileOutputStream out = new FileOutputStream(file);
    try {
        store.store(out, STORE_PASSWORD.toCharArray());
    } finally {
        Resources.close(out);
    }

    System.setProperty(CipherCodecFactory.Property.KEYSTORE_FILE.name, file.getPath());
    System.setProperty(CipherCodecFactory.Property.KEYSTORE_PASSWORD.name, STORE_PASSWORD);
    System.setProperty(CipherCodecFactory.Property.KEY_PASSWORD.name, KEY_PASSWORD);
}

From source file:com.creactiviti.piper.plugin.ffmpeg.Vstitch.java

@Override
public Object handle(Task aTask) throws Exception {
    List<String> chunks = aTask.getList("chunks", String.class);
    File tempFile = File.createTempFile("_chunks", ".txt");
    try {//from w  w  w .  ja va  2s.c om
        try (Writer writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(tempFile, true), "UTF-8"))) {
            for (String chunk : chunks) {
                writer.append(String.format("file '%s'", chunk)).append("\n");
            }
        }
        SimpleTaskExecution ffmpegTask = SimpleTaskExecution.create();
        List<String> options = Arrays.asList("-y", "-f", "concat", "-safe", "0", "-i",
                tempFile.getAbsolutePath(), "-c", "copy", aTask.getRequiredString("output"));
        ffmpegTask.set("options", options);
        ffmpeg.handle(ffmpegTask);
    } finally {
        FileUtils.deleteQuietly(tempFile);
    }
    return null;
}

From source file:controllers.PdfGenerator.java

public void addTemporaryFonts(List<String> fontsToLoad) {
    if (defaultFonts == null)
        defaultFonts = new ArrayList<String>();
    for (String font : fontsToLoad) {
        try {/*  w w  w .j  a  v a2  s  . co  m*/
            InputStream fin = Play.application().resourceAsStream(font);
            final File tempFile = File.createTempFile("tmp_" + FilenameUtils.getBaseName(font),
                    "." + FilenameUtils.getExtension(font));
            tempFile.deleteOnExit();
            FileOutputStream out = new FileOutputStream(tempFile);
            IOUtils.copy(fin, out);
            defaultFonts.add(tempFile.getAbsolutePath());
        } catch (Exception e) {
            Logger.error("Loading fonts", e);
        }
    }
}

From source file:com.msd.gin.halyard.tools.HalyardExportTest.java

@BeforeClass
public static void setup() throws Exception {
    File rf = File.createTempFile("HalyardExportTest", "");
    rf.delete();/* w w  w.  j ava  2s  . co  m*/
    rf.mkdirs();
    ROOT = rf.toURI().toURL().toString();
    if (!ROOT.endsWith("/")) {
        ROOT = ROOT + "/";
    }
    ValueFactory vf = SimpleValueFactory.getInstance();
    HBaseSail sail = new HBaseSail(HBaseServerTestInstance.getInstanceConfig(), TABLE, true, 0, true, 0, null);
    sail.initialize();
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            for (int k = 0; k < 10; k++) {
                sail.addStatement(vf.createIRI("http://whatever/subj" + i),
                        vf.createIRI("http://whatever/pred" + j), vf.createLiteral("whatever\n\"\\" + k));
            }
        }
    }
    sail.commit();
    sail.shutDown();
    HalyardExport.conf = HBaseServerTestInstance.getInstanceConfig();
}

From source file:com.whizzosoftware.hobson.rules.jruleengine.JRETaskProviderTest.java

@Test
public void testEmptyRuleConstruction() throws Exception {
    JRETaskProvider provider = new JRETaskProvider("pluginId");

    // validate we start with a non-existent temp file
    File ruleFile = File.createTempFile("rules", ".json");
    assertTrue(ruleFile.delete());/* w  ww  .  ja  v  a2 s. c  o  m*/
    assertFalse(ruleFile.exists());

    try {
        provider.setRulesFile(ruleFile);

        // make sure the provider created a new rule file
        assertTrue(ruleFile.exists());
        JSONObject json = new JSONObject(new JSONTokener(new FileReader(ruleFile)));
        assertPrefix(json);

        assertTrue(json.has("synonyms"));
        assertSynonyms(json.getJSONArray("synonyms"));

        assertFalse(json.has("rules"));
    } finally {
        assertTrue(ruleFile.delete());
    }
}

From source file:hr.fer.spocc.util.TokenListWriterTest.java

@Before
public void createTmpFile() throws IOException {
    tmpFile = File.createTempFile(RandomStringUtils.randomAlphabetic(3), null);
}

From source file:com.mengge.service.local.Scripts.java

public File getScriptFile() {
    InputStream inputStream = getClass().getResourceAsStream(RESOURCE_FOLDER + this.script);
    byte[] bytes;
    try {// w  w  w . j a  va2  s  . c  o m
        bytes = IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    String[] splittedName = this.script.split("\\.");
    File scriptFile;
    try {
        scriptFile = File.createTempFile(splittedName[0], "." + splittedName[1]);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    if (!scriptFile.exists()) {
        try {
            scriptFile.createNewFile();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    FileOutputStream output;
    try {
        output = new FileOutputStream(scriptFile, true);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }

    try {
        output.write(bytes);
        output.flush();
        output.close();
        return scriptFile;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.moss.blankslate.DerbyCatalogFactory.java

private static void init() throws Exception {
    if (hasInited == true)
        return;//from ww  w  . ja  va  2  s.co m
    hasInited = true;
    derbyHome = File.createTempFile("temp_derby_root", ".dir");
    derbyHome.delete();
    derbyHome.mkdir();
    derbyHome.deleteOnExit();
    log.info("Setting derby test home to " + derbyHome.getAbsolutePath());
    System.setProperty("derby.system.home", derbyHome.getAbsolutePath());
}

From source file:com.cloud.utils.PropertiesUtilsTest.java

@Test
public void loadPropertiesFromFile() throws IOException {
    File file = File.createTempFile("test", ".properties");
    FileUtils.writeStringToFile(file, "a=b\nc=d\n");
    Properties properties = PropertiesUtil.loadFromFile(file);
    Assert.assertEquals("b", properties.get("a"));
}