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.adaptris.core.fs.MarshallingCacheCase.java

@Override
public void setUp() throws Exception {
    persistentStore = File.createTempFile(this.getClass().getSimpleName(), "");
    persistentStore.delete();/*  www . j  a v  a 2s .co m*/
    super.setUp();
}

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

@Before
public void setup() throws IOException {
    pidFile = File.createTempFile("test", ".pid");
}

From source file:dotplugin.GraphViz.java

private static File execute(final InputStream input, String format) throws CoreException {
    MultiStatus status = new MultiStatus(Activator.ID, 0, "Errors occurred while running Graphviz", null);
    // we keep the input in memory so we can include it in error messages
    ByteArrayOutputStream dotContents = new ByteArrayOutputStream();
    File dotInput = null, dotOutput = null;
    try {// ww w. j  a  va  2s  . co m
        // determine the temp input and output locations
        dotInput = File.createTempFile(TMP_FILE_PREFIX, DOT_EXTENSION);
        dotOutput = File.createTempFile(TMP_FILE_PREFIX, "." + format);
        dotInput.deleteOnExit();
        dotOutput.deleteOnExit();
        FileOutputStream tmpDotInputStream = null;
        try {

            IOUtils.copy(input, dotContents);
            tmpDotInputStream = new FileOutputStream(dotInput);
            IOUtils.copy(new ByteArrayInputStream(dotContents.toByteArray()), tmpDotInputStream);
        } finally {

            IOUtils.closeQuietly(tmpDotInputStream);
        }

        IStatus result = runDot(format, dotInput, dotOutput);
        status.add(result);
        // status.add(logInput(dotContents));
        if (!result.isOK())
            LogUtils.log(status);
        return dotOutput;
    } catch (SWTException e) {
        status.add(new Status(IStatus.ERROR, Activator.ID, "", e));
    } catch (IOException e) {
        status.add(new Status(IStatus.ERROR, Activator.ID, "", e));
    }
    throw new CoreException(status);
}

From source file:com.mirth.connect.plugins.pdfviewer.PDFViewer.java

public void viewAttachments(String channelId, Long messageId, String attachmentId) {

    try {/*from   ww w . jav a  2  s  .c  o m*/
        Attachment attachment = parent.mirthClient.getAttachment(channelId, messageId, attachmentId);
        byte[] rawData = attachment.getContent();
        Base64InputStream in = new Base64InputStream(new ByteArrayInputStream(rawData));

        File temp = File.createTempFile(attachment.getId(), ".pdf");
        temp.deleteOnExit();

        OutputStream out = new FileOutputStream(temp);
        IOUtils.copy(in, out);
        out.close();

        new MirthPDFViewer(true, temp);

    } catch (Exception e) {
        parent.alertThrowable(parent, e);
    }
}

From source file:com.googlecode.xmlzen.XmlBuilderStreamOutputTest.java

@Test
public void testBuild() throws Exception {
    File myXml = File.createTempFile("xmlzentest", ".xml");
    OutputStream myXmlStream = new FileOutputStream(myXml);

    String xml = XmlBuilder.newXml(new XmlBuilderStreamOutput(myXmlStream, "UTF-8"), "UTF-8", false)
            .openTag("xml").withAttribute("id", 1).openTag("thisishow").withValue("you can build").closeTag()
            .openTag("your").withAttribute("xml", "nicely").withAttributeIf(1 < 0, "shouldnot", "happen")
            .toString(true);/*from  w w w . j a  va  2 s  . c o m*/
    log.debug(xml);
    myXmlStream.close();
    InputStreamReader reader = new InputStreamReader(new FileInputStream(myXml), "UTF-8");
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    int c = 0;
    while ((c = reader.read()) != -1) {
        bout.write(c);
    }
    reader.close();
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<xml id=\"1\"><thisishow>you can build</thisishow>" + "<your xml=\"nicely\"/></xml>",
            bout.toString("UTF-8"));
}

From source file:com.android.internal.http.multipart.MultipartTest.java

public void testParts() throws Exception {
    StringBuffer filebuffer = new StringBuffer();
    String filepartStr = "this is file part";
    filebuffer.append(filepartStr);/*ww w.  j  a  v  a 2s.  co  m*/
    File upload = File.createTempFile("Multipart", "test");

    FileWriter outFile = new FileWriter(upload);
    BufferedWriter out = new BufferedWriter(outFile);
    try {
        out.write(filebuffer.toString());
        out.flush();
    } finally {
        out.close();
    }

    Part[] parts = new Part[3];
    parts[0] = new StringPart("stringpart", "PART1!!");
    parts[1] = new FilePart(upload.getName(), upload);
    parts[2] = new StringPart("stringpart", "PART2!!");

    MultipartEntity me = new MultipartEntity(parts);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    me.writeTo(os);
    Header h = me.getContentType();
    String boundry = EncodingUtils.getAsciiString(me.getMultipartBoundary());
    StringBuffer contentType = new StringBuffer("multipart/form-data");
    contentType.append("; boundary=");
    contentType.append(boundry);
    assertEquals("Multipart content type error", contentType.toString(), h.getValue());
    final String CRLF = "\r\n";
    StringBuffer output = new StringBuffer();

    output.append("--");
    output.append(boundry);
    output.append(CRLF);

    output.append("Content-Disposition: form-data; name=\"stringpart\"");
    output.append(CRLF);
    output.append("Content-Type: text/plain; charset=US-ASCII");
    output.append(CRLF);
    output.append("Content-Transfer-Encoding: 8bit");
    output.append(CRLF);
    output.append(CRLF);
    output.append("PART1!!");
    output.append(CRLF);

    output.append("--");
    output.append(boundry);
    output.append(CRLF);

    output.append("Content-Disposition: form-data; name=\"");
    output.append(upload.getName());
    output.append("\"; filename=\"");
    output.append(upload.getName());
    output.append("\"");

    output.append(CRLF);
    output.append("Content-Type: application/octet-stream; charset=ISO-8859-1");
    output.append(CRLF);
    output.append("Content-Transfer-Encoding: binary");
    output.append(CRLF);
    output.append(CRLF);
    output.append(filepartStr);
    output.append(CRLF);

    output.append("--");
    output.append(boundry);
    output.append(CRLF);

    output.append("Content-Disposition: form-data; name=\"stringpart\"");
    output.append(CRLF);
    output.append("Content-Type: text/plain; charset=US-ASCII");
    output.append(CRLF);
    output.append("Content-Transfer-Encoding: 8bit");
    output.append(CRLF);
    output.append(CRLF);
    output.append("PART2!!");
    output.append(CRLF);

    output.append("--");
    output.append(boundry);
    output.append("--");
    output.append(CRLF);
    // System.out.print(output.toString());
    assertEquals("Multipart content error", output.toString(), os.toString());

    // System.out.print(os.toString());
}

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

public void testScript() throws IOException {

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

    String origScript = "Hello World";
    FileUtils.writeStringToFile(f, origScript, "UTF-8");
    PythonScript pyScript = new PythonScript(f);

    assertEquals(origScript, pyScript.getScript());
}

From source file:org.apache.camel.component.db4o.TestObjectContainerFactoryBean.java

public ObjectContainer getObject() throws Exception {
    File randomDatabase = File.createTempFile(getClass().getName(), "tmp");
    randomDatabase.deleteOnExit();// www.java 2 s.co m
    objectContainer = Db4oEmbedded.openFile(randomDatabase.getAbsolutePath());
    return objectContainer;
}

From source file:ZipUtilInPlaceTest.java

public void testAddEntry() throws IOException {
    File src = new File(getClass().getResource("demo.zip").getPath());
    File dest = File.createTempFile("temp.zip", null);
    try {/* ww  w  .ja v  a2s .  c  o  m*/
        FileUtils.copyFile(src, dest);

        final String fileName = "TestFile.txt";
        assertFalse(ZipUtil.containsEntry(dest, fileName));
        File newEntry = new File(getClass().getResource(fileName).getPath());

        ZipUtil.addEntry(dest, fileName, newEntry);
        assertTrue(ZipUtil.containsEntry(dest, fileName));
    } finally {
        FileUtils.deleteQuietly(dest);
    }
}

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

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