diff --git a/WHATSNEW b/WHATSNEW index d0f4e6e90..1de258964 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -28,6 +28,11 @@ Changes that could break older environments: * Remove fall-back mechanism for references that are not resolved during normal runtime execution. +* FileUtils.createTempFile now actually creates the file. + The TempFile task still does not create the file by default, can be instructed + to do so however using a new parameter. + Bugzilla report 33969. + Fixed bugs: ----------- diff --git a/docs/manual/CoreTasks/tempfile.html b/docs/manual/CoreTasks/tempfile.html index 696b4e297..54cefb680 100644 --- a/docs/manual/CoreTasks/tempfile.html +++ b/docs/manual/CoreTasks/tempfile.html @@ -144,7 +144,7 @@
The file denoted by the returned abstract pathname did not * exist before this method was invoked, any subsequent invocation * of this method will yield a different file name.
- *- * The filename is prefixNNNNNsuffix where NNNN is a random number. - *
- *This method is different from File.createTempFile() of JDK 1.2 - * as it doesn't create the file itself. It uses the location pointed - * to by java.io.tmpdir when the parentDir attribute is null.
+ * + *As of ant 1.8 the file is actually created.
* * @param prefix prefix before the random number. * @param suffix file extension; include the '.'. @@ -811,19 +807,15 @@ public class FileUtils { *The file denoted by the returned abstract pathname did not * exist before this method was invoked, any subsequent invocation * of this method will yield a different file name.
- *- * The filename is prefixNNNNNsuffix where NNNN is a random number. - *
- *This method is different from File.createTempFile() of JDK 1.2 - * as it doesn't create the file itself. It uses the location pointed - * to by java.io.tmpdir when the parentDir attribute is null.
+ * + *As of ant 1.8 the file is actually created.
* * @param prefix prefix before the random number. * @param suffix file extension; include the '.'. * @param parentDir Directory to create the temporary file in; + * java.io.tmpdir used if not specified. * @param deleteOnExit whether to set the tempfile for deletion on * normal VM exit. - * java.io.tmpdir used if not specified. * * @return a File reference to the new temporary file. * @since Ant 1.7 @@ -834,11 +826,51 @@ public class FileUtils { String parent = (parentDir == null) ? System.getProperty("java.io.tmpdir") : parentDir.getPath(); + + try { + result = File.createTempFile(prefix, suffix, new File(parent)); + } catch (IOException e) { + throw new BuildException("Could not create tempfile in " + parent, e); + } + + if (deleteOnExit) { + result.deleteOnExit(); + } + return result; + } + + /** + * Create a File object for a temporary file in a given directory. Without + * actually creating the file. + * + *The file denoted by the returned abstract pathname did not + * exist before this method was invoked, any subsequent invocation + * of this method will yield a different file name.
+ *+ * The filename is prefixNNNNNsuffix where NNNN is a random number. + *
+ * + * @param prefix prefix before the random number. + * @param suffix file extension; include the '.'. + * @param parentDir Directory to create the temporary file in; + * java.io.tmpdir used if not specified. + * @param deleteOnExit whether to set the tempfile for deletion on + * normal VM exit. + * + * @return a File reference to the new, nonexistent temporary file. + * @since Ant 1.8 + */ + public File createTempFileName(String prefix, String suffix, + File parentDir, boolean deleteOnExit) { + File result = null; + String parent = (parentDir == null) ? System + .getProperty("java.io.tmpdir") : parentDir.getPath(); DecimalFormat fmt = new DecimalFormat("#####"); synchronized (rand) { do { - result = new File(parent, prefix + fmt.format(Math.abs(rand.nextInt())) + suffix); + result = new File(parent, prefix + + fmt.format(Math.abs(rand.nextInt())) + suffix); } while (result.exists()); } if (deleteOnExit) { @@ -846,6 +878,8 @@ public class FileUtils { } return result; } + + /** * Compares the contents of two files.