Creating a Temporary File and delete it on exit
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; public class Main { public static void main(String[] argv) throws Exception { File temp = File.createTempFile("pattern", ".dat"); temp.deleteOnExit(); BufferedWriter out = new BufferedWriter(new FileWriter(temp)); out.write("asdf"); out.close(); } }