Creating a Temporary File

try {
    // Create temp file.
    File temp = File.createTempFile("pattern", ".suffix");

    // Delete temp file when program exits.
    temp.deleteOnExit();

    // Write to temp file
    BufferedWriter out = new BufferedWriter(new FileWriter(temp));
    out.write("aString");
    out.close();
} catch (IOException e) {
}

Comments

23 Sep 2011 - 1:58pm by Anonynus (not verified)

Great this was too useful for me.

16 Nov 2011 - 4:22am by morohbj (not verified)

delteOnExit causes a memory leak if your program runs for a long time as references to the file is kept within the jvm

23 Dec 2011 - 4:14am by Anonymous (not verified)

Thanks. Very Useful.

6 Apr 2012 - 12:31pm by Sladjana (not verified)

At last some raitnoailty in our little debate.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.