Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Creating a Temporary File Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!

Code:

 
 // -----------------------------------------------------------------------------
// CreateTemporaryFile.java
// -----------------------------------------------------------------------------

/*
 * =============================================================================
 * Copyright (c) 1998-2005 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, 2005 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();
    }

}

 
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy