Class IoUtil


  • public final class IoUtil
    extends java.lang.Object
    Input/Output, File and URL/URI related utilities.
    Version:
    $Id: IoUtil.java,v 1.9 2007/04/17 17:39:52 ddimon Exp $
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean compareFileDates​(java.util.Date date1, java.util.Date date2)
      For some reason modification milliseconds for some files are unstable, use this function to compare file dates ignoring milliseconds.
      static boolean compareFiles​(java.io.File file1, java.io.File file2)
      Compares two files for directories/files synchronization purposes.
      static void copyFile​(java.io.File src, java.io.File dest)
      Copies one file, existing file will be overridden.
      static void copyFolder​(java.io.File src, java.io.File dest)
      Copies folder recursively, existing files will be overridden
      static void copyFolder​(java.io.File src, java.io.File dest, boolean recursive)
      Copies folder, existing files will be overridden
      static void copyFolder​(java.io.File src, java.io.File dest, boolean recursive, boolean onlyNew)
      Copies folder.
      static void copyFolder​(java.io.File src, java.io.File dest, boolean recursive, boolean onlyNew, java.io.FileFilter filter)
      Copies folder.
      static void copyStream​(java.io.InputStream in, java.io.OutputStream out, int bufferSize)
      Copies streams.
      static boolean emptyFolder​(java.io.File folder)
      Recursively deletes whole content of the given folder.
      static java.net.URL file2url​(java.io.File file)
      Utility method to convert a File object to a local URL.
      static java.io.InputStream getResourceInputStream​(java.net.URL url)
      Opens input stream for given resource.
      static boolean isResourceExists​(java.net.URL url)
      Checks if resource exist and can be opened.
      static void synchronizeFolders​(java.io.File src, java.io.File dest)
      Performs one-way directories synchronization comparing files only, not folders.
      static void synchronizeFolders​(java.io.File src, java.io.File dest, java.io.FileFilter filter)
      Performs one-way directories synchronization comparing files only, not folders.
      static java.io.File url2file​(java.net.URL url)
      Utility method to convert local URL to a File object.
      • Methods inherited from class java.lang.Object

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

      • copyFile

        public static void copyFile​(java.io.File src,
                                    java.io.File dest)
                             throws java.io.IOException
        Copies one file, existing file will be overridden.
        Parameters:
        src - source file to copy FROM
        dest - destination file to copy TO
        Throws:
        java.io.IOException - if any I/O error has occurred
      • copyFolder

        public static void copyFolder​(java.io.File src,
                                      java.io.File dest)
                               throws java.io.IOException
        Copies folder recursively, existing files will be overridden
        Parameters:
        src - source folder
        dest - target folder
        Throws:
        java.io.IOException - if any I/O error has occurred
      • copyFolder

        public static void copyFolder​(java.io.File src,
                                      java.io.File dest,
                                      boolean recursive)
                               throws java.io.IOException
        Copies folder, existing files will be overridden
        Parameters:
        src - source folder
        dest - target folder
        recursive - if true, processes folder recursively
        Throws:
        java.io.IOException - if any I/O error has occurred
      • copyFolder

        public static void copyFolder​(java.io.File src,
                                      java.io.File dest,
                                      boolean recursive,
                                      boolean onlyNew)
                               throws java.io.IOException
        Copies folder.
        Parameters:
        src - source folder
        dest - target folder
        recursive - if true, processes folder recursively
        onlyNew - if true, target file will be overridden if it is older than source file only
        Throws:
        java.io.IOException - if any I/O error has occurred
      • copyFolder

        public static void copyFolder​(java.io.File src,
                                      java.io.File dest,
                                      boolean recursive,
                                      boolean onlyNew,
                                      java.io.FileFilter filter)
                               throws java.io.IOException
        Copies folder.
        Parameters:
        src - source folder
        dest - target folder
        recursive - if true, processes folder recursively
        onlyNew - if true, target file will be overridden if it is older than source file only
        filter - file filter, optional, if null all files will be copied
        Throws:
        java.io.IOException - if any I/O error has occurred
      • copyStream

        public static void copyStream​(java.io.InputStream in,
                                      java.io.OutputStream out,
                                      int bufferSize)
                               throws java.io.IOException
        Copies streams.
        Parameters:
        in - source stream
        out - destination stream
        bufferSize - buffer size to use
        Throws:
        java.io.IOException - if any I/O error has occurred
      • emptyFolder

        public static boolean emptyFolder​(java.io.File folder)
        Recursively deletes whole content of the given folder.
        Parameters:
        folder - folder to be emptied
        Returns:
        true if given folder becomes empty or not exists
      • compareFiles

        public static boolean compareFiles​(java.io.File file1,
                                           java.io.File file2)
        Compares two files for directories/files synchronization purposes.
        Parameters:
        file1 - one file to compare
        file2 - another file to compare
        Returns:
        true if file names are equal (case sensitive), files have equal lengths and modification dates (milliseconds ignored)
        See Also:
        synchronizeFolders(File, File), compareFileDates(Date, Date)
      • compareFileDates

        public static boolean compareFileDates​(java.util.Date date1,
                                               java.util.Date date2)
        For some reason modification milliseconds for some files are unstable, use this function to compare file dates ignoring milliseconds.
        Parameters:
        date1 - first file modification date
        date2 - second file modification date
        Returns:
        true if files modification dates are equal ignoring milliseconds
      • synchronizeFolders

        public static void synchronizeFolders​(java.io.File src,
                                              java.io.File dest)
                                       throws java.io.IOException
        Performs one-way directories synchronization comparing files only, not folders.
        Parameters:
        src - source folder
        dest - target folder
        Throws:
        java.io.IOException - if any I/O error has occurred
        See Also:
        synchronizeFolders(File, File, FileFilter), compareFiles(File, File)
      • synchronizeFolders

        public static void synchronizeFolders​(java.io.File src,
                                              java.io.File dest,
                                              java.io.FileFilter filter)
                                       throws java.io.IOException
        Performs one-way directories synchronization comparing files only, not folders.
        Parameters:
        src - source folder
        dest - target folder
        filter - file filter, optional, if null all files will be included into synchronization process
        Throws:
        java.io.IOException - if any I/O error has occurred
        See Also:
        compareFiles(File, File)
      • isResourceExists

        public static boolean isResourceExists​(java.net.URL url)
        Checks if resource exist and can be opened.
        Parameters:
        url - absolute URL which points to a resource to be checked
        Returns:
        true if given URL points to an existing resource
      • getResourceInputStream

        public static java.io.InputStream getResourceInputStream​(java.net.URL url)
                                                          throws java.io.IOException
        Opens input stream for given resource. This method behaves differently for different URL types:
        • for local files it returns buffered file input stream;
        • for local JAR files it reads resource content into memory buffer and returns byte array input stream that wraps those buffer (this prevents locking JAR file);
        • for common URL's this method simply opens stream to that URL using standard URL API.
        It is not recommended to use this method for big resources within JAR files.
        Parameters:
        url - resource URL
        Returns:
        input stream for given resource
        Throws:
        java.io.IOException - if any I/O error has occurred
      • url2file

        public static java.io.File url2file​(java.net.URL url)
        Utility method to convert local URL to a File object.
        Parameters:
        url - an URL
        Returns:
        file object for given URL or null if URL is not local
      • file2url

        public static java.net.URL file2url​(java.io.File file)
                                     throws java.net.MalformedURLException
        Utility method to convert a File object to a local URL.
        Parameters:
        file - a file object
        Returns:
        absolute URL that points to the given file
        Throws:
        java.net.MalformedURLException - if file can't be represented as URL for some reason