Class FileUtils


  • public class FileUtils
    extends java.lang.Object
    Utility class used for file manipulation operations.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copyDir​(java.io.File fromDir, java.io.File toDir, boolean overwrite)
      Copies the provided File.
      static boolean copyFile​(java.io.File fromFile, java.io.File toFile, boolean overwrite)
      Copy file.
      static boolean copyFile​(java.lang.String fromPath, java.lang.String toPath, boolean overwrite)
      Copy file.
      static void delete​(java.io.File file)
      Simply calls the delete() method of the provided file object.
      static void delete​(java.io.File file, Log log)
      Simply calls the delete() method of the provided file object.
      static void delete​(java.lang.String path)
      Simply calls the delete() method of the provided file object.
      static void delete​(java.lang.String path, Log log)
      Simply calls the delete() method of the provided file object.
      static void deleteRecursively​(java.io.File toDelete)
      Deletes the provided File.
      static void deleteRecursively​(java.lang.String toDelete)
      Convenience method that delegates to deleteRecursively(File).
      static java.lang.String loadFile​(java.io.File file)
      Loads the specified file and returns its content as String.
      static java.lang.String loadFile​(java.io.File file, java.lang.String encoding)
      Loads the specified file and returns its content as String.
      static java.lang.String loadFile​(java.lang.String file)
      Loads the specified file and returns its content as String.
      static void mkdir​(java.io.File file)
      Simply calls the mkdir() method of the provided file object.
      static void mkdir​(java.io.File file, Log log)
      Simply calls the mkdir() method of the provided file object.
      static void mkdir​(java.lang.String path)
      Simply calls the mkdir() method of the provided file object.
      static void mkdir​(java.lang.String path, Log log)
      Simply calls the mkdir() method of the provided file object.
      static void mkdirs​(java.io.File file)
      Simply calls the mkdirs() method of the provided file object.
      static void mkdirs​(java.io.File file, Log log)
      Simply calls the mkdirs() method of the provided file object.
      static void mkdirs​(java.lang.String path)
      Simply calls the mkdirs() method of the provided file object.
      static void mkdirs​(java.lang.String path, Log log)
      Simply calls the mkdirs() method of the provided file object.
      static byte[] readInputStream​(java.io.InputStream is)
      Reads the provided input stream and returns it as an array of bytes.
      static void renameTo​(java.io.File from, java.io.File to)
      Simply calls the renameTo() method of the provided file object.
      static void renameTo​(java.io.File from, java.io.File to, Log log)
      Simply calls the renameTo() method of the provided file object.
      static void renameTo​(java.lang.String from, java.lang.String to)
      Simply calls the renameTo() method of the provided file object.
      static void renameTo​(java.lang.String from, java.lang.String to, Log log)
      Simply calls the renameTo() method of the provided file object.
      • Methods inherited from class java.lang.Object

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

      • FileUtils

        public FileUtils()
    • Method Detail

      • loadFile

        public static java.lang.String loadFile​(java.lang.String file)
                                         throws java.lang.Exception
        Loads the specified file and returns its content as String.
        Parameters:
        file - the path to the file to load.
        Returns:
        the content of the file.
        Throws:
        java.lang.Exception - if an I/O error occurs.
      • loadFile

        public static java.lang.String loadFile​(java.io.File file)
                                         throws java.lang.Exception
        Loads the specified file and returns its content as String. The default platform character encoding will be used for this operation.
        Parameters:
        file - the file to load.
        Returns:
        the content of the file.
        Throws:
        java.lang.Exception - if an I/O error occurs.
      • loadFile

        public static java.lang.String loadFile​(java.io.File file,
                                                java.lang.String encoding)
                                         throws java.lang.Exception
        Loads the specified file and returns its content as String. The specified character encoding will be used for this operation.
        Parameters:
        file - the file to load.
        encoding - the name of the encoding to use, if this is null the platform default encoding will be used.
        Returns:
        the content of the file.
        Throws:
        java.lang.Exception - if an I/O error occurs.
      • readInputStream

        public static byte[] readInputStream​(java.io.InputStream is)
                                      throws java.io.IOException
        Reads the provided input stream and returns it as an array of bytes. This method does not invoke the close() method on the provided InputStream object.
        Parameters:
        is - the input stream to read
        Returns:
        the bites from the input stream.
        Throws:
        java.io.IOException - if reading error occurs.
      • delete

        public static void delete​(java.io.File file,
                                  Log log)
        Simply calls the delete() method of the provided file object. Additionally the returned value is checked and logged if the file could not be deleted.
        Parameters:
        file - the file to delete
        log -
        Throws:
        java.lang.IllegalArgumentException - - if either of the parameters is null or the specified file does not exists.
        java.lang.SecurityException - if the file could not be deleted.
      • delete

        public static void delete​(java.lang.String path,
                                  Log log)
        Simply calls the delete() method of the provided file object. Additionally the returned value is checked and logged if the file could not be deleted.
        Parameters:
        path - the file to delete
        log -
        Throws:
        java.lang.IllegalArgumentException - - if either of the parameters is null.
        java.lang.SecurityException - if the file could not be deleted.
        java.lang.NullPointerException - if the provided path argument is null.
      • mkdirs

        public static void mkdirs​(java.io.File file,
                                  Log log)
        Simply calls the mkdirs() method of the provided file object. Additionally the returned value is checked and logged if the directories could not be created.
        Parameters:
        file - the directory to create
        log -
        Throws:
        java.lang.IllegalArgumentException - - if either of the parameters is null
        java.lang.SecurityException - if the directories could not be created
      • mkdirs

        public static void mkdirs​(java.lang.String path,
                                  Log log)
        Simply calls the mkdirs() method of the provided file object. Additionally the returned value is checked and logged if the directories could not be created.
        Parameters:
        path - the directory to create
        log -
        Throws:
        java.lang.IllegalArgumentException - - if either of the parameters is null.
        java.lang.SecurityException - if the directories could not be created
        java.lang.NullPointerException - if the provided path argument is null.
      • mkdir

        public static void mkdir​(java.io.File file,
                                 Log log)
        Simply calls the mkdir() method of the provided file object. Additionally the returned value is checked and logged if the directory could not be created.
        Parameters:
        file - the directory to create
        log -
        Throws:
        java.lang.IllegalArgumentException - - if either of the parameters is null
        java.lang.SecurityException - if the directories could not be created
      • mkdir

        public static void mkdir​(java.lang.String path,
                                 Log log)
        Simply calls the mkdir() method of the provided file object. Additionally the returned value is checked and logged if the directories could not be created.
        Parameters:
        path - the directory to create
        log -
        Throws:
        java.lang.IllegalArgumentException - - if either of the parameters is null.
        java.lang.SecurityException - if the directory could not be created
        java.lang.NullPointerException - if the provided path argument is null.
      • renameTo

        public static void renameTo​(java.io.File from,
                                    java.io.File to,
                                    Log log)
        Simply calls the renameTo() method of the provided file object. Additionally the returned value is checked and logged if the file could not be renamed.
        Parameters:
        from - the file to rename
        to - the name of the file to set
        log -
        Throws:
        java.lang.IllegalArgumentException - - if either of the parameters is null or the specified file does not renamed.
        java.lang.SecurityException - if the file could not be deleted.
      • renameTo

        public static void renameTo​(java.lang.String from,
                                    java.lang.String to,
                                    Log log)
        Simply calls the renameTo() method of the provided file object. Additionally the returned value is checked and logged if the file could not be renamed.
        Parameters:
        from - the file to rename
        to - the name of the file to set
        log -
        Throws:
        java.lang.IllegalArgumentException - - if either of the parameters is null.
        java.lang.SecurityException - if the file could not be renamed.
        java.lang.NullPointerException - if the provided path argument is null.
      • delete

        public static void delete​(java.io.File file)
                           throws java.lang.Exception
        Simply calls the delete() method of the provided file object. Additionally the returned value is checked and an exception is thrown if the file could not be deleted.
        Parameters:
        file - the file to delete
        Throws:
        java.lang.Exception - if the file cannot be deleted.
        java.lang.IllegalArgumentException - - if either of the parameters is null or the specified file does not exists.
        java.lang.SecurityException - if the file could not be deleted.
      • deleteRecursively

        public static void deleteRecursively​(java.io.File toDelete)
                                      throws java.lang.Exception
        Deletes the provided File. If the File denotes a directory it will be deleted recursively whit all of the files it contains. Additionally the returned value is checked and an exception is thrown if the file could not be deleted.
        Parameters:
        file - the file to delete
        Throws:
        java.lang.Exception - if the file cannot be deleted.
        java.lang.IllegalArgumentException - - if either of the parameters is null or the specified file does not exists.
        java.lang.SecurityException - if the file could not be deleted.
      • deleteRecursively

        public static void deleteRecursively​(java.lang.String toDelete)
                                      throws java.lang.Exception
        Convenience method that delegates to deleteRecursively(File).
        Parameters:
        toDelete - the file to delete
        Throws:
        java.lang.Exception - if the file cannot be deleted.
      • delete

        public static void delete​(java.lang.String path)
                           throws java.lang.Exception
        Simply calls the delete() method of the provided file object. Additionally the returned value is checked and an exception is thrown if the file could not be deleted.
        Parameters:
        path - the file to delete
        Throws:
        java.lang.Exception - if the file could not be deleted.
        java.lang.IllegalArgumentException - - if either of the parameters is null.
        java.lang.SecurityException - if the file could not be deleted.
        java.lang.NullPointerException - if the provided path argument is null.
      • mkdirs

        public static void mkdirs​(java.io.File file)
                           throws java.lang.Exception
        Simply calls the mkdirs() method of the provided file object. Additionally the returned value is checked and an exception is thrown if the directories could not be created.
        Parameters:
        file - the directory to create
        Throws:
        java.lang.Exception - if the directories could not be created.
        java.lang.IllegalArgumentException - - if either of the parameters is null
        java.lang.SecurityException - if the directories could not be created
      • mkdirs

        public static void mkdirs​(java.lang.String path)
                           throws java.lang.Exception
        Simply calls the mkdirs() method of the provided file object. Additionally the returned value is checked and an exception is thrown if the directories could not be created.
        Parameters:
        path - the directory to create
        Throws:
        java.lang.Exception - if the directories could not be created.
        java.lang.IllegalArgumentException - - if either of the parameters is null.
        java.lang.SecurityException - if the directories could not be created
        java.lang.NullPointerException - if the provided path argument is null.
      • mkdir

        public static void mkdir​(java.io.File file)
                          throws java.lang.Exception
        Simply calls the mkdir() method of the provided file object. Additionally the returned value is checked and an exception is thrown if the directory could not be created.
        Parameters:
        file - the directory to create
        Throws:
        java.lang.Exception - if the directory could not be created.
        java.lang.IllegalArgumentException - - if either of the parameters is null
        java.lang.SecurityException - if the directories could not be created
      • mkdir

        public static void mkdir​(java.lang.String path)
                          throws java.lang.Exception
        Simply calls the mkdir() method of the provided file object. Additionally the returned value is checked and an exception is thrown if the directories could not be created.
        Parameters:
        path - the directory to create
        Throws:
        java.lang.Exception
        java.lang.IllegalArgumentException - - if either of the parameters is null.
        java.lang.SecurityException - if the directory could not be created
        java.lang.NullPointerException - if the provided path argument is null.
      • renameTo

        public static void renameTo​(java.io.File from,
                                    java.io.File to)
                             throws java.lang.Exception
        Simply calls the renameTo() method of the provided file object. Additionally the returned value is checked and an exception is thrown if the file could not be renamed.
        Parameters:
        from - the file to rename
        to - the name of the file to set
        Throws:
        java.lang.Exception - if the file could not be renamed.
        java.lang.IllegalArgumentException - - if either of the parameters is null or the specified file does not renamed.
        java.lang.SecurityException - if the file could not be renamed.
      • renameTo

        public static void renameTo​(java.lang.String from,
                                    java.lang.String to)
                             throws java.lang.Exception
        Simply calls the renameTo() method of the provided file object. Additionally the returned value is checked and an exception is thrown if the file could not be renamed.
        Parameters:
        from - the file to rename
        to - the name of the file to set
        Throws:
        java.lang.Exception - if the file could not be renamed.
        java.lang.IllegalArgumentException - - if either of the parameters is null.
        java.lang.SecurityException - if the file could not be renamed.
        java.lang.NullPointerException - if the provided path argument is null.
      • copyFile

        public static boolean copyFile​(java.lang.String fromPath,
                                       java.lang.String toPath,
                                       boolean overwrite)
                                throws java.lang.Exception
        Copy file. This method copies fromPath to toPath. The overwrite flag specifies whether the destination file should be overwritten. This method is called by com.ibm.di.function.UserFunctions.copyFile(String, String, boolean) method.
        Parameters:
        fromPath - The source file
        toPath - The destination file
        overwrite - Specify true if destination should be overwritten.
        Returns:
        true if file was copied, false if toPath exists and overwrite=false.
        Throws:
        java.lang.Exception
      • copyFile

        public static boolean copyFile​(java.io.File fromFile,
                                       java.io.File toFile,
                                       boolean overwrite)
                                throws java.lang.Exception
        Copy file. This method copies fromPath to toPath. The overwrite flag specifies whether the destination file should be overwritten. This method is called by com.ibm.di.function.UserFunctions.copyFile(File, File, boolean) method.

        Parameters:
        fromFile - The source file
        toFile - The destination file
        overwrite - Specify true if destination should be overwritten.
        Returns:
        true if file was copied, false if toPath exists and overwrite=false.
        Throws:
        java.lang.Exception
      • copyDir

        public static void copyDir​(java.io.File fromDir,
                                   java.io.File toDir,
                                   boolean overwrite)
                            throws java.lang.Exception
        Copies the provided File. The directory will be copied recursively whit all of the files it contains.
        Parameters:
        fromDir - the directory to copy from
        toDir - the directory to copy to (created if missing)
        overwrite - if toDir exists all of its conflicting children will be overwritten.
        Throws:
        java.lang.Exception - if a directory cannot be created.
        java.lang.IllegalArgumentException - - if either of the parameters is null or the specified file does not exists.
        java.lang.SecurityException - if the file could not be deleted.