// ----------------------------------------------------------------------------- // CreateTemporaryFile.java // ----------------------------------------------------------------------------- /* * ============================================================================= * Copyright (c) 1998-2009 Jeffrey M. Hunter. All rights reserved. * * All source code and material located at the Internet address of * http://www.idevelopment.info is the copyright of Jeffrey M. Hunter and * is protected under copyright laws of the United States. This source code may * not be hosted on any other site without my express, prior, written * permission. Application to host any of the material elsewhere can be made by * contacting me at jhunter@idevelopment.info. * * I have made every effort and taken great care in making sure that the source * code and other content included on my web site is technically accurate, but I * disclaim any and all responsibility for any loss, damage or destruction of * data or any other property which may arise from relying on it. I will in no * case be liable for any monetary damages arising from such loss, damage or * destruction. * * As with any code, ensure to test this code in a development environment * before attempting to run it in production. * ============================================================================= */ import java.io.File; import java.io.FileWriter; import java.io.BufferedWriter; import java.io.IOException; /** * ----------------------------------------------------------------------------- * This program demonstrates how to create a temporary file that will be * deleted when the program exits. * * The Java I/O package contains a method for creating temporary files. The * method creates a new empty file in the specified directory, using the given * prefix and suffix strings to generate its name. If this method returns * successfully then it is guaranteed that: * * 1.) The file denoted by the returned abstract pathname did not exist * before this method was invoked, and * 2.) Neither this method nor any of its variants will return the same * abstract pathname again in the current invocation of the virtual * machine. * * This method provides only part of a temporary-file facility. To arrange for * a file created by this method to be deleted automatically, use the * deleteOnExit() method. * * The prefix argument must be at least three characters long. It is * recommended that the prefix be a short, meaningful string such as "hjb" or * "mail". The suffix argument may be null, in which case the suffix ".tmp" * will be used. * * To create the new file, the prefix and the suffix may first be adjusted to * fit the limitations of the underlying platform. If the prefix is too long * then it will be truncated, but its first three characters will always be * preserved. If the suffix is too long then it too will be truncated, but if * it begins with a period character ('.') then the period and the first three * characters following it will always be preserved. Once these adjustments * have been made the name of the new file will be generated by concatenating * the prefix, five or more internally-generated characters, and the suffix. * * If the directory argument is null then the system-dependent default * temporary-file directory will be used. The default temporary-file directory * is specified by the system property java.io.tmpdir. On UNIX systems the * default value of this property is typically "/tmp" or "/var/tmp"; on * Win32 systems it is typically "c:\\temp". A different value may be given to * this system property when the Java virtual machine is invoked, but * programmatic changes to this property are not guaranteed to have any effect * upon the the temporary directory used by this method. * * Parameters: * prefix - The prefix string to be used in generating the file's * name; must be at least three characters long * suffix - The suffix string to be used in generating the file's * name; may be null, in which case the suffix ".tmp" will * be used * directory - The directory in which the file is to be created, or * null if the default temporary-file directory is to be * used * Returns: * An abstract pathname denoting a newly-created empty file * * @version 1.0 * @author Jeffrey M. Hunter (jhunter@idevelopment.info) * @author http://www.idevelopment.info * ----------------------------------------------------------------------------- */ public class CreateTemporaryFile { private static void doCreate() { try { // Create a temporary file object File tempFile = File.createTempFile("mail", "temp"); System.out.println("\nCreated temporary file: " + tempFile + "\n"); // Delete temp file when program exits tempFile.deleteOnExit(); // Write to temporary file BufferedWriter out = new BufferedWriter(new FileWriter(tempFile)); out.write("TEMP FILE: " + tempFile); out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * Sole entry point to the class and application. * @param args Array of String arguments. */ public static void main(String[] args) { doCreate(); } }