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.vityuk.ginger.loader.FileSystemResourceLoaderTest.java

@Test
public void testOpenWithExistingLocation() throws IOException {
    String data = "test data";

    File file = File.createTempFile(FileSystemResourceLoaderTest.class.getSimpleName(), ".data");
    InputStream inputStream = null;
    try {/*from w  w  w  .  j a v  a2  s  . com*/
        FileUtils.write(file, data);

        inputStream = loader.openStream("file:" + file.getAbsolutePath());

        String actualData = IOUtils.toString(inputStream);
        assertThat(actualData).isEqualTo(data);
    } finally {
        Closeables.closeQuietly(inputStream);
        file.delete();
    }
}

From source file:com.googlecode.jsonschema2pojo.integration.ref.AbsoluteRefIT.java

private File createSchemaWithAbsoluteRef() throws IOException {

    URL absoluteUrlForAddressSchema = this.getClass().getResource("/schema/ref/address.json");

    String absoluteRefSchemaTemplate = IOUtils
            .toString(this.getClass().getResourceAsStream("/schema/ref/absoluteRef.json.template"));
    String absoluteRefSchema = absoluteRefSchemaTemplate.replace("$ABSOLUTE_REF",
            absoluteUrlForAddressSchema.toString());

    File absoluteRefSchemaFile = File.createTempFile("absoluteRef", ".json");

    try {/*from  w  w  w  . ja v  a  2  s .  c om*/
        FileOutputStream outputStream = new FileOutputStream(absoluteRefSchemaFile);
        try {
            IOUtils.write(absoluteRefSchema, outputStream);
        } finally {
            IOUtils.closeQuietly(outputStream);
        }
    } finally {
        absoluteRefSchemaFile.deleteOnExit();
    }

    return absoluteRefSchemaFile;

}

From source file:eu.europa.ejusticeportal.dss.applet.model.action.SavePdfActionTest.java

/**
 * Test the doExec method for SavePdfAction
 *//* w w w.  j  a v  a 2 s.c o  m*/
@Test
public void testDoExec() {
    FileInputStream fis = null;
    FileInputStream result = null;
    SavePdfAction instance;
    try {
        fis = new FileInputStream("src/test/resources/hello-world.pdf");
        byte[] pdf = (IOUtils.toByteArray(fis));
        instance = new SavePdfAction(pdf, null);
        instance.exec();

        File f = File.createTempFile("test", ".pdf");
        f.deleteOnExit();
        instance = new SavePdfAction(pdf, f);
        instance.exec();
        assertNotNull(f);
        assertTrue(f.exists());
        assertTrue(f.canRead());
        assertTrue(f.canWrite());

        result = new FileInputStream(f);
        byte[] resultPdf = (IOUtils.toByteArray(result));
        assertTrue(resultPdf.length > 0);

    } catch (FileNotFoundException ex) {
        fail("Hello-world.pdf is not available.");
    } catch (IOException ex) {
        fail("IO Issue");
    } finally {
        try {
            fis.close();
            result.close();
        } catch (IOException ex) {
            fail("Unable to close File stream");
        }
    }
}

From source file:net.sourceforge.dita4publishers.word2dita.DocxUpdaterTest.java

public void testParseWithXMLFormatLoggingXMLReader() throws Exception {
    File messageFile = File.createTempFile("DocxUpdaterTest-", ".xml");
    File docxFile = new File(docxUrl.toURI());
    File newDocxFile = File.createTempFile("docxupdater", ".docx");
    assertTrue("DOCX file does not exist", docxFile.exists());
    URL inputUrl = topic_1_1Url;//from w w  w . j a va2s .  co  m

    Document logDoc = Word2DitaValidationHelper.validateXml(messageFile, inputUrl, catalogs);

    Word2DitaValidationHelper.addValidationMessagesToDocxFile(docxFile, newDocxFile, logDoc);

    System.out.println("New zip file is: " + newDocxFile.getAbsolutePath());
}

From source file:com.simas.vc.background_tasks.FFprobe.java

/**
 * This operation is synchronous and cannot be run on the UI thread.
 *///from  w ww.j  ava 2s.  c o m
@SuppressWarnings("ResultOfMethodCallIgnored")
public synchronized static FileAttributes parseAttributes(File inputFile) throws VCException {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        throw new IllegalStateException("parseAttributes cannot be run on the UI thread!");
    }

    /* Executable call used
       ./ffprobe -i 'nature/bee.mp4' \
       -v quiet -print_format json \
       -show_format -show_entries format=duration,size,format_name,format_long_name,filename,nb_streams \
       -show_streams -show_entries stream=codec_name,codec_long_name,codec_type,sample_rate,channels,duration,display_aspect_ratio,width,height,time_base,codec_time_base,r_frame_rate
    */

    // Check if input exists
    if (!inputFile.exists()) {
        throw new VCException(Utils.getString(R.string.input_not_found));
    }

    // Create a temporary file to hold the stdout output
    File tmpFile;
    try {
        tmpFile = File.createTempFile("vc-out", null);
        tmpFile.delete();
        tmpFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
        throw new VCException(Utils.getString(R.string.tmp_not_created));
    }

    // Create arguments for ffprobe
    final String[] args = new ArgumentBuilder(TAG).add("-i").addSpaced("%s", inputFile.getPath()) // Spaced input file path
            .add("-v quiet -print_format json") // Output quietly in JSON
            // Format entries to show
            .add("-show_format -show_entries format=%s,%s,%s,%s,%s,%s",
                    Utils.getString(R.string.format_duration), Utils.getString(R.string.format_size),
                    Utils.getString(R.string.format_name), Utils.getString(R.string.format_long_name),
                    Utils.getString(R.string.format_filename), Utils.getString(R.string.format_stream_count))
            // Stream entries to show
            .add("-show_streams -show_entries stream=%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",
                    Utils.getString(R.string.stream_name), Utils.getString(R.string.stream_long_name),
                    Utils.getString(R.string.stream_type), Utils.getString(R.string.stream_sample_rate),
                    Utils.getString(R.string.stream_channels), Utils.getString(R.string.stream_duration),
                    Utils.getString(R.string.stream_aspect_ratio), Utils.getString(R.string.stream_width),
                    Utils.getString(R.string.stream_height), Utils.getString(R.string.stream_tbn),
                    Utils.getString(R.string.stream_tbc), Utils.getString(R.string.stream_tbr),
                    Utils.getString(R.string.stream_codec_tag))
            .build();

    if (cFFprobe(args, tmpFile.getPath()) != 0) {
        throw new VCException(Utils.getString(R.string.ffprobe_fail));
    }

    BufferedReader reader = null;
    FileAttributes fa = null;
    try {
        // Parse file
        reader = new BufferedReader(new FileReader(tmpFile));
        StringBuilder sb = new StringBuilder();
        String line = reader.readLine();

        while (line != null) {
            sb.append(line);
            sb.append('\n');
            line = reader.readLine();
        }
        String content = sb.toString();

        int firstOpeningBrace = content.indexOf('{');
        int lastClosingBrace = content.lastIndexOf('}');
        if (firstOpeningBrace == -1 || lastClosingBrace == -1) {
            return null;
        }
        String json = content.substring(firstOpeningBrace, lastClosingBrace + 1);

        // Parse JSON
        fa = parseJsonAttributes(json);
        Log.i(TAG, "Parsed attributes: " + fa);
    } catch (IOException e) {
        e.printStackTrace();
        throw new VCException(Utils.getString(R.string.ffprobe_fail));
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // Delete the tmp file
    tmpFile.delete();

    // No support for files with no audio/video streams (for now?)
    if (fa != null) {
        if (fa.getAudioStreams().size() == 0) {
            throw new VCException(Utils.getString(R.string.audioless_unsupported));
        } else if (fa.getVideoStreams().size() == 0) {
            throw new VCException(Utils.getString(R.string.videoless_unsupported));
        }
    }

    return fa;
}