How to create Temp file in java?

DeeKay August 27, 2011 at 11:15 pm · java.io

Below java code shows how to create Temp File in java

<br />
/****************************************************************************************<br />
 * Created on 08-2011 Copyright(c) http://kodehelp.com All Rights Reserved.<br />
 ****************************************************************************************/<br />
package com.kodehelp.javaio;</p>
<p>import java.io.File;<br />
import java.io.FileOutputStream;<br />
import java.io.IOException;<br />
import java.io.PrintStream;</p>
<p>/**<br />
 * Created by http://kodehelp.com<br />
 * Date: 8/27/11<br />
 */<br />
public class CreateTempFile {<br />
    public static void main(String args[]){<br />
        try {<br />
            File tempFile = File.createTempFile("demo",".tmp");<br />
            FileOutputStream fout = new FileOutputStream(tempFile);<br />
            PrintStream out = new PrintStream(fout);<br />
            out.println("This is sample text in the temporary file which is created");<br />
        } catch (IOException e) {<br />
            e.printStackTrace();<br />
        }</p>
<p>    }<br />
}<br />