src/share/classes/java/io/File.java

Print this page




1983      *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
1984      *          method does not allow a file to be created
1985      *
1986      * @since 1.2
1987      */
1988     public static File createTempFile(String prefix, String suffix,
1989                                       File directory)
1990         throws IOException
1991     {
1992         if (prefix.length() < 3)
1993             throw new IllegalArgumentException("Prefix string too short");
1994         if (suffix == null)
1995             suffix = ".tmp";
1996 
1997         File tmpdir = (directory != null) ? directory
1998                                           : TempDirectory.location();
1999         File f;
2000         try {
2001             do {
2002                 f = TempDirectory.generateFile(prefix, suffix, tmpdir);
2003             } while (f.exists());
2004             if (!f.createNewFile())
2005                 throw new IOException("Unable to create temporary file");
2006         } catch (SecurityException se) {
2007             // don't reveal temporary directory location
2008             if (directory == null)
2009                 throw new SecurityException("Unable to create temporary file");
2010             throw se;
2011         }
2012         return f;
2013     }
2014 
2015     /**
2016      * Creates an empty file in the default temporary-file directory, using
2017      * the given prefix and suffix to generate its name. Invoking this method
2018      * is equivalent to invoking <code>{@link #createTempFile(java.lang.String,
2019      * java.lang.String, java.io.File)
2020      * createTempFile(prefix,&nbsp;suffix,&nbsp;null)}</code>.
2021      *
2022      * <p> The {@link
2023      * java.nio.file.Files#createTempFile(String,String,java.nio.file.attribute.FileAttribute[])




1983      *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
1984      *          method does not allow a file to be created
1985      *
1986      * @since 1.2
1987      */
1988     public static File createTempFile(String prefix, String suffix,
1989                                       File directory)
1990         throws IOException
1991     {
1992         if (prefix.length() < 3)
1993             throw new IllegalArgumentException("Prefix string too short");
1994         if (suffix == null)
1995             suffix = ".tmp";
1996 
1997         File tmpdir = (directory != null) ? directory
1998                                           : TempDirectory.location();
1999         File f;
2000         try {
2001             do {
2002                 f = TempDirectory.generateFile(prefix, suffix, tmpdir);
2003             } while ((fs.getBooleanAttributes(f) & FileSystem.BA_EXISTS) != 0);
2004             if (!f.createNewFile())
2005                 throw new IOException("Unable to create temporary file");
2006         } catch (SecurityException se) {
2007             // don't reveal temporary directory location
2008             if (directory == null)
2009                 throw new SecurityException("Unable to create temporary file");
2010             throw se;
2011         }
2012         return f;
2013     }
2014 
2015     /**
2016      * Creates an empty file in the default temporary-file directory, using
2017      * the given prefix and suffix to generate its name. Invoking this method
2018      * is equivalent to invoking <code>{@link #createTempFile(java.lang.String,
2019      * java.lang.String, java.io.File)
2020      * createTempFile(prefix,&nbsp;suffix,&nbsp;null)}</code>.
2021      *
2022      * <p> The {@link
2023      * java.nio.file.Files#createTempFile(String,String,java.nio.file.attribute.FileAttribute[])