Class FileCopyUtils

java.lang.Object
com.aspectran.utils.FileCopyUtils

public class FileCopyUtils extends Object

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

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.
  • Constructor Details

    • FileCopyUtils

      public FileCopyUtils()
  • Method Details

    • copyFileToDirectory

      public static void copyFileToDirectory(File srcFile, File destDir) throws 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:
      NullPointerException - if source or destination is null
      IOException - if source or destination is invalid
      IOException - if an IO error occurs during copying
      See Also:
    • copyFileToDirectory

      public static void copyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate) throws 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:
      NullPointerException - if source or destination is null
      IOException - if source or destination is invalid
      IOException - if an IO error occurs during copying
      See Also:
    • copyFile

      public static void copyFile(File srcFile, File destFile) throws 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:
      NullPointerException - if source or destination is null
      IOException - if source or destination is invalid
      IOException - if an IO error occurs during copying
      See Also:
    • copyFile

      public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws 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:
      NullPointerException - if source or destination is null
      IOException - if source or destination is invalid
      IOException - if an IO error occurs during copying
      See Also:
    • copyFile

      public static long copyFile(File input, OutputStream output) throws 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:
      NullPointerException - if the input or output is null
      IOException - if an I/O error occurs
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir) throws 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:
      NullPointerException - if source or destination is null
      IOException - if source or destination is invalid
      IOException - if an IO error occurs during copying
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate) throws 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:
      NullPointerException - if source or destination is null
      IOException - if source or destination is invalid
      IOException - if an IO error occurs during copying
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir, FileFilter filter) throws 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:
      NullPointerException - if source or destination is null
      IOException - if source or destination is invalid
      IOException - if an IO error occurs during copying
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate) throws 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:
      NullPointerException - if source or destination is null
      IOException - if source or destination is invalid
      IOException - if an IO error occurs during copying