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

/****************************************************************************************
 * Created on 08-2011 Copyright(c) http://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.javaio;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
 * Created by http://kodehelp.com
 * Date: 8/27/11
 */
public class CreateTempFile {
    public static void main(String args[]){
        try {
            File tempFile = File.createTempFile("demo",".tmp");
            FileOutputStream fout = new FileOutputStream(tempFile);
            PrintStream out = new PrintStream(fout);
            out.println("This is sample text in the temporary file which is created");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}