Create a temporary file

From CodeCodex

Implementations[edit]

Java[edit]

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

Python[edit]

import tempfile, os
tempfd, tempname = tempfile.mkstemp('.suffix')
os.write(tempfd, "aString") # or, if you want a file-object: os.fdopen(tempfd, 'w+')
os.close(tempfd)
os.unlink(tempname)

Ruby[edit]

require 'tempfile'

path = Tempfile.open("prefix") do |fp|
  fp.puts "aString"
  fp.path       # return path
end             # In the end of the block, a temporary file is closed.

p path                  # Display a path to temporary file.
p File.read(path)       #=> "aString\n"