Creating a Temp File

Introduction

Every Operating System has it’s own location for storing temporary files. In UNIX/Linux this is /tmp. In Windows it’s C:\temp.

The example below shows how it can be done in the (platform independent way with Java:

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) {
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.