Java生成随机名称的文件

import java.io.File;
File.createTempFile(prefix,suffix)

它生成的文件具有唯一性,并且作为temp会在虚拟机关闭的时候自动删除
具体功能参考doc:

/**
 * <p> 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:
 *
 * <ol>
 * <li> The file denoted by the returned abstract pathname did not exist
 *      before this method was invoked, and
 * <li> Neither this method nor any of its variants will return the same
 *      abstract pathname again in the current invocation of the virtual
 *      machine.
 * </ol>
 *
 * 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
 * <code>{@link #deleteOnExit}</code> method.
 *
 * <p> The <code>prefix</code> argument must be at least three characters
 * long.  It is recommended that the prefix be a short, meaningful string
 * such as <code>"hjb"</code> or <code>"mail"</code>.  The
 * <code>suffix</code> argument may be <code>null</code>, in which case the
 * suffix <code>".tmp"</code> will be used.
 *
 * <p> 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
 * (<code>'.'</code>) 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.
 *
 * <p> If the <code>directory</code> argument is <code>null</code> then the
 * system-dependent default temporary-file directory will be used.  The
 * default temporary-file directory is specified by the system property
 * <code>java.io.tmpdir</code>.  On UNIX systems the default value of this
 * property is typically <code>"/tmp"</code> or <code>"/var/tmp"</code>; on
 * Microsoft Windows systems it is typically <code>"C:\\WINNT\\TEMP"</code>.  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 temporary directory used by this method.
 *
 * @param  prefix     The prefix string to be used in generating the file's
 *                    name; must be at least three characters long
 *
 * @param  suffix     The suffix string to be used in generating the file's
 *                    name; may be <code>null</code>, in which case the
 *                    suffix <code>".tmp"</code> will be used
 *
 * @param  directory  The directory in which the file is to be created, or
 *                    <code>null</code> if the default temporary-file
 *                    directory is to be used
 *
 * @return  An abstract pathname denoting a newly-created empty file
 *
 * @throws  IllegalArgumentException
 *          If the <code>prefix</code> argument contains fewer than three
 *          characters
 *
 * @throws  IOException  If a file could not be created
 *
 * @throws  SecurityException
 *          If a security manager exists and its <code>{@link
 *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
 *          method does not allow a file to be created
 *
 * @since 1.2
 */
public static File createTempFile(String prefix, String suffix,
        File directory)
        throws IOException
        {
         ...   
        }