767
768
public static void copyDirectory(File srcDir, File destDir,
768
769
boolean preserveFileDate) throws IOException {
770
copyDirectory(srcDir, destDir, null, preserveFileDate);
774
* Copies a filtered directory to a new location preserving the file dates.
776
* This method copies the contents of the specified source directory
777
* to within the specified destination directory.
779
* The destination directory is created if it does not exist.
780
* If the destination directory did exist, then this method merges
781
* the source with the destination, with the source taking precedence.
783
* <h4>Example: Copy directories only</h4>
785
* // only copy the directory structure
786
* FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY);
789
* <h4>Example: Copy directories and txt files</h4>
791
* // Create a filter for ".txt" files
792
* IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
793
* IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
795
* // Create a filter for either directories or ".txt" files
796
* FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
798
* // Copy using the filter
799
* FileUtils.copyDirectory(srcDir, destDir, filter);
802
* @param srcDir an existing directory to copy, must not be <code>null</code>
803
* @param destDir the new directory, must not be <code>null</code>
804
* @param filter the filter to apply, null means copy all directories and files
805
* should be the same as the original
807
* @throws NullPointerException if source or destination is <code>null</code>
808
* @throws IOException if source or destination is invalid
809
* @throws IOException if an IO error occurs during copying
810
* @since Commons IO 1.4
812
public static void copyDirectory(File srcDir, File destDir,
813
FileFilter filter) throws IOException {
814
copyDirectory(srcDir, destDir, filter, true);
818
* Copies a filtered directory to a new location.
820
* This method copies the contents of the specified source directory
821
* to within the specified destination directory.
823
* The destination directory is created if it does not exist.
824
* If the destination directory did exist, then this method merges
825
* the source with the destination, with the source taking precedence.
827
* <h4>Example: Copy directories only</h4>
829
* // only copy the directory structure
830
* FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);
833
* <h4>Example: Copy directories and txt files</h4>
835
* // Create a filter for ".txt" files
836
* IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
837
* IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
839
* // Create a filter for either directories or ".txt" files
840
* FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
842
* // Copy using the filter
843
* FileUtils.copyDirectory(srcDir, destDir, filter, false);
846
* @param srcDir an existing directory to copy, must not be <code>null</code>
847
* @param destDir the new directory, must not be <code>null</code>
848
* @param filter the filter to apply, null means copy all directories and files
849
* @param preserveFileDate true if the file date of the copy
850
* should be the same as the original
852
* @throws NullPointerException if source or destination is <code>null</code>
853
* @throws IOException if source or destination is invalid
854
* @throws IOException if an IO error occurs during copying
855
* @since Commons IO 1.4
857
public static void copyDirectory(File srcDir, File destDir,
858
FileFilter filter, boolean preserveFileDate) throws IOException {
769
859
if (srcDir == null) {
770
860
throw new NullPointerException("Source must not be null");
810
916
throw new IOException("Destination '" + destDir + "' cannot be written to");
813
File[] files = srcDir.listFiles();
919
File[] files = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
814
920
if (files == null) { // null if security restricted
815
921
throw new IOException("Failed to list contents of " + srcDir);
817
923
for (int i = 0; i < files.length; i++) {
818
924
File copiedFile = new File(destDir, files[i].getName());
819
if (files[i].isDirectory()) {
820
doCopyDirectory(files[i], copiedFile, preserveFileDate);
822
doCopyFile(files[i], copiedFile, preserveFileDate);
925
if (exclusionList == null || !exclusionList.contains(files[i].getCanonicalPath())) {
926
if (files[i].isDirectory()) {
927
doCopyDirectory(files[i], copiedFile, filter, preserveFileDate, exclusionList);
929
doCopyFile(files[i], copiedFile, preserveFileDate);
1567
1710
return checksum;
1714
* Moves a directory.
1716
* When the destination directory is on another file system, do a "copy and delete".
1718
* @param srcDir the directory to be moved
1719
* @param destDir the destination directory
1720
* @throws NullPointerException if source or destination is <code>null</code>
1721
* @throws IOException if source or destination is invalid
1722
* @throws IOException if an IO error occurs moving the file
1723
* @since Commons IO 1.4
1725
public static void moveDirectory(File srcDir, File destDir) throws IOException {
1726
if (srcDir == null) {
1727
throw new NullPointerException("Source must not be null");
1729
if (destDir == null) {
1730
throw new NullPointerException("Destination must not be null");
1732
if (!srcDir.exists()) {
1733
throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
1735
if (!srcDir.isDirectory()) {
1736
throw new IOException("Source '" + srcDir + "' is not a directory");
1738
if (destDir.exists()) {
1739
throw new IOException("Destination '" + destDir + "' already exists");
1741
boolean rename = srcDir.renameTo(destDir);
1743
copyDirectory( srcDir, destDir );
1744
deleteDirectory( srcDir );
1745
if (srcDir.exists()) {
1746
throw new IOException("Failed to delete original directory '" + srcDir +
1747
"' after copy to '" + destDir + "'");
1753
* Moves a directory to another directory.
1755
* @param src the file to be moved
1756
* @param destDir the destination file
1757
* @param createDestDir If <code>true</code> create the destination directory,
1758
* otherwise if <code>false</code> throw an IOException
1759
* @throws NullPointerException if source or destination is <code>null</code>
1760
* @throws IOException if source or destination is invalid
1761
* @throws IOException if an IO error occurs moving the file
1762
* @since Commons IO 1.4
1764
public static void moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) throws IOException {
1766
throw new NullPointerException("Source must not be null");
1768
if (destDir == null) {
1769
throw new NullPointerException("Destination directory must not be null");
1771
if (!destDir.exists() && createDestDir) {
1774
if (!destDir.exists()) {
1775
throw new FileNotFoundException("Destination directory '" + destDir +
1776
"' does not exist [createDestDir=" + createDestDir +"]");
1778
if (!destDir.isDirectory()) {
1779
throw new IOException("Destination '" + destDir + "' is not a directory");
1781
moveDirectory(src, new File(destDir, src.getName()));
1788
* When the destination file is on another file system, do a "copy and delete".
1790
* @param srcFile the file to be moved
1791
* @param destFile the destination file
1792
* @throws NullPointerException if source or destination is <code>null</code>
1793
* @throws IOException if source or destination is invalid
1794
* @throws IOException if an IO error occurs moving the file
1795
* @since Commons IO 1.4
1797
public static void moveFile(File srcFile, File destFile) throws IOException {
1798
if (srcFile == null) {
1799
throw new NullPointerException("Source must not be null");
1801
if (destFile == null) {
1802
throw new NullPointerException("Destination must not be null");
1804
if (!srcFile.exists()) {
1805
throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
1807
if (srcFile.isDirectory()) {
1808
throw new IOException("Source '" + srcFile + "' is a directory");
1810
if (destFile.exists()) {
1811
throw new IOException("Destination '" + destFile + "' already exists");
1813
if (destFile.isDirectory()) {
1814
throw new IOException("Destination '" + destFile + "' is a directory");
1816
boolean rename = srcFile.renameTo(destFile);
1818
copyFile( srcFile, destFile );
1819
if (!srcFile.delete()) {
1820
FileUtils.deleteQuietly(destFile);
1821
throw new IOException("Failed to delete original file '" + srcFile +
1822
"' after copy to '" + destFile + "'");
1828
* Moves a file to a directory.
1830
* @param srcFile the file to be moved
1831
* @param destDir the destination file
1832
* @param createDestDir If <code>true</code> create the destination directory,
1833
* otherwise if <code>false</code> throw an IOException
1834
* @throws NullPointerException if source or destination is <code>null</code>
1835
* @throws IOException if source or destination is invalid
1836
* @throws IOException if an IO error occurs moving the file
1837
* @since Commons IO 1.4
1839
public static void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir) throws IOException {
1840
if (srcFile == null) {
1841
throw new NullPointerException("Source must not be null");
1843
if (destDir == null) {
1844
throw new NullPointerException("Destination directory must not be null");
1846
if (!destDir.exists() && createDestDir) {
1849
if (!destDir.exists()) {
1850
throw new FileNotFoundException("Destination directory '" + destDir +
1851
"' does not exist [createDestDir=" + createDestDir +"]");
1853
if (!destDir.isDirectory()) {
1854
throw new IOException("Destination '" + destDir + "' is not a directory");
1856
moveFile(srcFile, new File(destDir, srcFile.getName()));
1860
* Moves a file or directory to the destination directory.
1862
* When the destination is on another file system, do a "copy and delete".
1864
* @param src the file or directory to be moved
1865
* @param destDir the destination directory
1866
* @param createDestDir If <code>true</code> create the destination directory,
1867
* otherwise if <code>false</code> throw an IOException
1868
* @throws NullPointerException if source or destination is <code>null</code>
1869
* @throws IOException if source or destination is invalid
1870
* @throws IOException if an IO error occurs moving the file
1871
* @since Commons IO 1.4
1873
public static void moveToDirectory(File src, File destDir, boolean createDestDir) throws IOException {
1875
throw new NullPointerException("Source must not be null");
1877
if (destDir == null) {
1878
throw new NullPointerException("Destination must not be null");
1880
if (!src.exists()) {
1881
throw new FileNotFoundException("Source '" + src + "' does not exist");
1883
if (src.isDirectory()) {
1884
moveDirectoryToDirectory(src, destDir, createDestDir);
1886
moveFileToDirectory(src, destDir, createDestDir);