Skip to content

Commit 71a72d8

Browse files
author
Stephen Colebourne
committed
New FileUtils method to copy a directory to within another directory
rfe 36315 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@369070 13f79535-47bb-0310-9956-ffa450edef68
1 parent 37bf562 commit 71a72d8

3 files changed

Lines changed: 65 additions & 4 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,18 @@ Enhancements from 1.1
5050

5151
- FileUtils.lineIterator
5252
IOUtils.lineIterator
53-
New methods to provide an iterator over the lines in a file
53+
New methods to provide an iterator over the lines in a file [38083]
54+
55+
- FileUtils.copyDirectoryToDirectory
56+
New method to copy a directory to within another directory [36315]
5457

5558

5659
Feedback
5760
--------
58-
Open source works best when you give feedback.
61+
Open source works best when you give feedback:
5962
http://jakarta.apache.org/commons/io/
6063

61-
Please direct all bug reports to Bugzilla.
64+
Please direct all bug reports to Bugzilla (prefix bug reports by [io])
6265
http://issues.apache.org/bugzilla/buglist.cgi?product=Commons&component=IO
6366

6467
Or subscribe to the commons-user mailing list (prefix emails by [io])

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,13 +578,49 @@ private static void doCopyFile(File srcFile, File destFile, boolean preserveFile
578578
}
579579

580580
//-----------------------------------------------------------------------
581+
/**
582+
* Copies a directory to within another directory preserving the file dates.
583+
* <p>
584+
* This method copies the source directory and all its contents to a
585+
* directory of the same name in the specified destination directory.
586+
* <p>
587+
* The destination directory is created if it does not exist.
588+
* If the destination directory did exist, then this method merges
589+
* the source with the destination, with the source taking precedence.
590+
*
591+
* @param srcDir an existing directory to copy, must not be null
592+
* @param destDir the directory to place the copy in, must not be null
593+
*
594+
* @throws NullPointerException if source or destination is null
595+
* @throws IOException if source or destination is invalid
596+
* @throws IOException if an IO error occurs during copying
597+
*/
598+
public static void copyDirectoryToDirectory(File srcDir, File destDir) throws IOException {
599+
if (srcDir == null) {
600+
throw new NullPointerException("Source must not be null");
601+
}
602+
if (srcDir.exists() && srcDir.isDirectory() == false) {
603+
throw new IllegalArgumentException("Source '" + destDir + "' is not a directory");
604+
}
605+
if (destDir == null) {
606+
throw new NullPointerException("Destination must not be null");
607+
}
608+
if (destDir.exists() && destDir.isDirectory() == false) {
609+
throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");
610+
}
611+
copyDirectory(srcDir, new File(destDir, srcDir.getName()), true);
612+
}
613+
581614
/**
582615
* Copies a whole directory to a new location preserving the file dates.
583616
* <p>
584617
* This method copies the specified directory and all its child
585618
* directories and files to the specified destination.
586619
* The destination is the new location and name of the directory.
587-
* If it already exists, the contents will be overwritten.
620+
* <p>
621+
* The destination directory is created if it does not exist.
622+
* If the destination directory did exist, then this method merges
623+
* the source with the destination, with the source taking precedence.
588624
*
589625
* @param srcDir an existing directory to copy, must not be null
590626
* @param destDir the new directory, must not be null
@@ -603,6 +639,7 @@ public static void copyDirectory(File srcDir, File destDir) throws IOException {
603639
* <p>
604640
* This method copies the contents of the specified source directory
605641
* to within the specified destination directory.
642+
* <p>
606643
* The destination directory is created if it does not exist.
607644
* If the destination directory did exist, then this method merges
608645
* the source with the destination, with the source taking precedence.

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,27 @@ public void testCopyFile2WithoutFileDatePreservation() throws Exception {
431431
testFile1.lastModified() != destination.lastModified());*/
432432
}
433433

434+
public void testCopyDirectoryToDirectory_NonExistingDest() throws Exception {
435+
createFile(testFile1, 1234);
436+
createFile(testFile2, 4321);
437+
File srcDir = getTestDirectory();
438+
File subDir = new File(srcDir, "sub");
439+
subDir.mkdir();
440+
File subFile = new File(subDir, "A.txt");
441+
FileUtils.writeStringToFile(subFile, "HELLO WORLD", "UTF8");
442+
File destDir = new File(System.getProperty("java.io.tmpdir"), "tmp-FileUtilsTestCase");
443+
FileUtils.deleteDirectory(destDir);
444+
File actualDestDir = new File(destDir, srcDir.getName());
445+
446+
FileUtils.copyDirectoryToDirectory(srcDir, destDir);
447+
448+
assertTrue("Check exists", destDir.exists());
449+
assertTrue("Check exists", actualDestDir.exists());
450+
assertEquals("Check size", FileUtils.sizeOfDirectory(srcDir), FileUtils.sizeOfDirectory(actualDestDir));
451+
assertEquals(true, new File(actualDestDir, "sub/A.txt").exists());
452+
FileUtils.deleteDirectory(destDir);
453+
}
454+
434455
public void testCopyDirectoryToNonExistingDest() throws Exception {
435456
createFile(testFile1, 1234);
436457
createFile(testFile2, 4321);

0 commit comments

Comments
 (0)