Class FileCopyUtils


  • public class FileCopyUtils
    extends java.lang.Object
    Simple utility methods for file and directory copying. All copy methods use a block size of 4096 bytes, and close all affected streams when done.

    This class is a clone of org.apache.commons.io.FileUtils

    • Constructor Summary

      Constructors 
      Constructor Description
      FileCopyUtils()
      Instances should NOT be constructed in standard programming.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copyDirectory​(java.io.File srcDir, java.io.File destDir)
      Copies a whole directory to a new location preserving the file dates.
      static void copyDirectory​(java.io.File srcDir, java.io.File destDir, boolean preserveFileDate)
      Copies a whole directory to a new location.
      static void copyDirectory​(java.io.File srcDir, java.io.File destDir, java.io.FileFilter filter)
      Copies a filtered directory to a new location preserving the file dates.
      static void copyDirectory​(java.io.File srcDir, java.io.File destDir, java.io.FileFilter filter, boolean preserveFileDate)
      Copies a filtered directory to a new location.
      static void copyFile​(java.io.File srcFile, java.io.File destFile)
      Copies a file to a new location preserving the file date.
      static void copyFile​(java.io.File srcFile, java.io.File destFile, boolean preserveFileDate)
      Copies a file to a new location.
      static long copyFile​(java.io.File input, java.io.OutputStream output)
      Copy bytes from a File to an OutputStream.
      static void copyFileToDirectory​(java.io.File srcFile, java.io.File destDir)
      Copies a file to a directory preserving the file date.
      static void copyFileToDirectory​(java.io.File srcFile, java.io.File destDir, boolean preserveFileDate)
      Copies a file to a directory optionally preserving the file date.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileCopyUtils

        public FileCopyUtils()
        Instances should NOT be constructed in standard programming.
    • Method Detail

      • copyFileToDirectory

        public static void copyFileToDirectory​(java.io.File srcFile,
                                               java.io.File destDir)
                                        throws java.io.IOException
        Copies a file to a directory preserving the file date.

        This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.

        Note: This method tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        srcFile - an existing file to copy, must not be null
        destDir - the directory to place the copy in, must not be null
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs during copying
        See Also:
        copyFile(File, File, boolean)
      • copyFileToDirectory

        public static void copyFileToDirectory​(java.io.File srcFile,
                                               java.io.File destDir,
                                               boolean preserveFileDate)
                                        throws java.io.IOException
        Copies a file to a directory optionally preserving the file date.

        This method copies the contents of the specified source file to a file of the same name in the specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will overwrite it.

        Note: Setting preserveFileDate to true tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        srcFile - an existing file to copy, must not be null
        destDir - the directory to place the copy in, must not be null
        preserveFileDate - true if the file date of the copy should be the same as the original
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs during copying
        See Also:
        copyFile(File, File, boolean)
      • copyFile

        public static void copyFile​(java.io.File srcFile,
                                    java.io.File destFile)
                             throws java.io.IOException
        Copies a file to a new location preserving the file date.

        This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

        Note: This method tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        srcFile - an existing file to copy, must not be null
        destFile - the new file, must not be null
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs during copying
        See Also:
        copyFileToDirectory(File, File), copyFile(File, File, boolean)
      • copyFile

        public static void copyFile​(java.io.File srcFile,
                                    java.io.File destFile,
                                    boolean preserveFileDate)
                             throws java.io.IOException
        Copies a file to a new location.

        This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

        Note: Setting preserveFileDate to true tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        srcFile - an existing file to copy, must not be null
        destFile - the new file, must not be null
        preserveFileDate - true if the file date of the copy should be the same as the original
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs during copying
        See Also:
        copyFileToDirectory(File, File, boolean), doCopyFile(File, File, boolean)
      • copyFile

        public static long copyFile​(java.io.File input,
                                    java.io.OutputStream output)
                             throws java.io.IOException
        Copy bytes from a File to an OutputStream.
        Parameters:
        input - the File to read from
        output - the OutputStream to write to
        Returns:
        the number of bytes copied
        Throws:
        java.lang.NullPointerException - if the input or output is null
        java.io.IOException - if an I/O error occurs
      • copyDirectory

        public static void copyDirectory​(java.io.File srcDir,
                                         java.io.File destDir)
                                  throws java.io.IOException
        Copies a whole directory to a new location preserving the file dates.

        This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory.

        The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

        Note: This method tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        srcDir - an existing directory to copy, must not be null
        destDir - the new directory, must not be null
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs during copying
      • copyDirectory

        public static void copyDirectory​(java.io.File srcDir,
                                         java.io.File destDir,
                                         boolean preserveFileDate)
                                  throws java.io.IOException
        Copies a whole directory to a new location.

        This method copies the contents of the specified source directory to within the specified destination directory.

        The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

        Note: Setting preserveFileDate to true tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        srcDir - an existing directory to copy, must not be null
        destDir - the new directory, must not be null
        preserveFileDate - true if the file date of the copy should be the same as the original
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs during copying
      • copyDirectory

        public static void copyDirectory​(java.io.File srcDir,
                                         java.io.File destDir,
                                         java.io.FileFilter filter)
                                  throws java.io.IOException
        Copies a filtered directory to a new location preserving the file dates.

        This method copies the contents of the specified source directory to within the specified destination directory.

        The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

        Note: This method tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.

        Example: Copy directories only
          // only copy the directory structure
          FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY);
         
        Example: Copy directories and txt files
          // Create a filter for ".txt" files
          IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt");
          IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
        
          // Create a filter for either directories or ".txt" files
          FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
        
          // Copy using the filter
          FileCopyUtils.copyDirectory(srcDir, destDir, filter);
         
        Parameters:
        srcDir - an existing directory to copy, must not be null
        destDir - the new directory, must not be null
        filter - the filter to apply, null means copy all directories and files should be the same as the original
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs during copying
      • copyDirectory

        public static void copyDirectory​(java.io.File srcDir,
                                         java.io.File destDir,
                                         java.io.FileFilter filter,
                                         boolean preserveFileDate)
                                  throws java.io.IOException
        Copies a filtered directory to a new location.

        This method copies the contents of the specified source directory to within the specified destination directory.

        The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

        Note: Setting preserveFileDate to true tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        srcDir - an existing directory to copy, must not be null
        destDir - the new directory, must not be null
        filter - the filter to apply, null means copy all directories and files
        preserveFileDate - true if the file date of the copy should be the same as the original
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs during copying