This method does not guarantee that the + * operation is atomic.
+ * + * @since 1.21, Ant 1.5 + */ + public boolean createNewFile(File f) throws IOException { + if (f != null) { + if (f.exists()) { + return false; + } + + FileOutputStream fos = null; + try { + fos = new FileOutputStream(f); + fos.write(new byte[0]); + } finally { + if (fos != null) { + fos.close(); + } + } + + return true; + } + return false; + } + } diff --git a/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java b/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java index 5b7e01166..66dbaf5d2 100644 --- a/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java +++ b/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java @@ -372,6 +372,16 @@ public class FileUtilsTest extends TestCase { new File("docs.xml"))); } + /** + * Test createNewFile + */ + public void testCreateNewFile() throws IOException { + removeThis = new File("dummy"); + assertTrue(!removeThis.exists()); + fu.createNewFile(removeThis); + assertTrue(removeThis.exists()); + } + /** * adapt file separators to local conventions */