Skip to content

Commit 587571d

Browse files
committed
[IO-361] Add API FileUtils.forceMkdirsParent().
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1423916 13f79535-47bb-0310-9956-ffa450edef68
1 parent ef58d20 commit 587571d

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="201?-??-??" description="New features and bug fixes.">
50+
<action issue="IO-361" dev="ggregory" type="add">
51+
Add API FileUtils.forceMkdirsParent().
52+
</action>
5053
<action issue="IO-360" dev="ggregory" type="add">
5154
Add API Charsets.requiredCharsets().
5255
</action>

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,26 @@ public static void forceMkdir(final File directory) throws IOException {
24212421
}
24222422
}
24232423

2424+
/**
2425+
* Makes any necessary but nonexistent parent directories for a given File. If the parent directory cannot be
2426+
* created then an IOException is thrown.
2427+
*
2428+
* @param file
2429+
* file with parent to create, must not be {@code null}
2430+
* @throws NullPointerException
2431+
* if the file is {@code null}
2432+
* @throws IOException
2433+
* if the parent directory cannot be created
2434+
* @since 2.5
2435+
*/
2436+
public static void forceMkdirParent(final File file) throws IOException {
2437+
final File parent = file.getParentFile();
2438+
if (parent == null) {
2439+
return;
2440+
}
2441+
forceMkdir(parent);
2442+
}
2443+
24242444
//-----------------------------------------------------------------------
24252445
/**
24262446
* Returns the size of the specified file or directory. If the provided

src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,29 @@ public void testForceMkdir() throws Exception {
746746
assertTrue("Directory was not created.", testFile.exists());
747747
}
748748

749+
public void testForceMkdirParent() throws Exception {
750+
// Tests with existing directory
751+
assertTrue(getTestDirectory().exists());
752+
final File testParentDir = new File(getTestDirectory(), "testForceMkdirParent");
753+
try {
754+
testParentDir.delete();
755+
assertFalse(testParentDir.exists());
756+
final File testFile = new File(testParentDir, "test.txt");
757+
assertFalse(testParentDir.exists());
758+
assertFalse(testFile.exists());
759+
// Create
760+
FileUtils.forceMkdirParent(testFile);
761+
assertTrue(testParentDir.exists());
762+
assertFalse(testFile.exists());
763+
// Again
764+
FileUtils.forceMkdirParent(testFile);
765+
assertTrue(testParentDir.exists());
766+
assertFalse(testFile.exists());
767+
} finally {
768+
testParentDir.delete();
769+
}
770+
}
771+
749772
// sizeOfDirectory
750773

751774
public void testSizeOfDirectory() throws Exception {

0 commit comments

Comments
 (0)