Author: ggregory Date: Wed Dec 19 16:17:26 2012 New Revision: 1423916 URL: http://svn.apache.org/viewvc?rev=1423916&view=rev Log: [IO-361] Add API FileUtils.forceMkdirsParent().
Modified: commons/proper/io/trunk/src/changes/changes.xml commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Modified: commons/proper/io/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1423916&r1=1423915&r2=1423916&view=diff ============================================================================== --- commons/proper/io/trunk/src/changes/changes.xml (original) +++ commons/proper/io/trunk/src/changes/changes.xml Wed Dec 19 16:17:26 2012 @@ -47,6 +47,9 @@ The <action> type attribute can be add,u <body> <!-- The release date is the date RC is cut --> <release version="2.5" date="201?-??-??" description="New features and bug fixes."> + <action issue="IO-361" dev="ggregory" type="add"> + Add API FileUtils.forceMkdirsParent(). + </action> <action issue="IO-360" dev="ggregory" type="add"> Add API Charsets.requiredCharsets(). </action> Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1423916&r1=1423915&r2=1423916&view=diff ============================================================================== --- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original) +++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Wed Dec 19 16:17:26 2012 @@ -2421,6 +2421,26 @@ public class FileUtils { } } + /** + * Makes any necessary but nonexistent parent directories for a given File. If the parent directory cannot be + * created then an IOException is thrown. + * + * @param file + * file with parent to create, must not be {@code null} + * @throws NullPointerException + * if the file is {@code null} + * @throws IOException + * if the parent directory cannot be created + * @since 2.5 + */ + public static void forceMkdirParent(final File file) throws IOException { + final File parent = file.getParentFile(); + if (parent == null) { + return; + } + forceMkdir(parent); + } + //----------------------------------------------------------------------- /** * Returns the size of the specified file or directory. If the provided Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1423916&r1=1423915&r2=1423916&view=diff ============================================================================== --- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java (original) +++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Wed Dec 19 16:17:26 2012 @@ -746,6 +746,29 @@ public class FileUtilsTestCase extends F assertTrue("Directory was not created.", testFile.exists()); } + public void testForceMkdirParent() throws Exception { + // Tests with existing directory + assertTrue(getTestDirectory().exists()); + final File testParentDir = new File(getTestDirectory(), "testForceMkdirParent"); + try { + testParentDir.delete(); + assertFalse(testParentDir.exists()); + final File testFile = new File(testParentDir, "test.txt"); + assertFalse(testParentDir.exists()); + assertFalse(testFile.exists()); + // Create + FileUtils.forceMkdirParent(testFile); + assertTrue(testParentDir.exists()); + assertFalse(testFile.exists()); + // Again + FileUtils.forceMkdirParent(testFile); + assertTrue(testParentDir.exists()); + assertFalse(testFile.exists()); + } finally { + testParentDir.delete(); + } + } + // sizeOfDirectory public void testSizeOfDirectory() throws Exception {