Class IoUtil

java.lang.Object
org.java.plugin.util.IoUtil

public final class IoUtil extends 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 Details

    • copyFile

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

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

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

      public static void copyFolder(File src, File dest, boolean recursive, boolean onlyNew) throws 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:
      IOException - if any I/O error has occurred
    • copyFolder

      public static void copyFolder(File src, File dest, boolean recursive, boolean onlyNew, FileFilter filter) throws 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:
      IOException - if any I/O error has occurred
    • copyStream

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

      public static boolean emptyFolder(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(File file1, 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:
    • compareFileDates

      public static boolean compareFileDates(Date date1, 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(File src, File dest) throws IOException
      Performs one-way directories synchronization comparing files only, not folders.
      Parameters:
      src - source folder
      dest - target folder
      Throws:
      IOException - if any I/O error has occurred
      See Also:
    • synchronizeFolders

      public static void synchronizeFolders(File src, File dest, FileFilter filter) throws 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:
      IOException - if any I/O error has occurred
      See Also:
    • isResourceExists

      public static boolean isResourceExists(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 InputStream getResourceInputStream(URL url) throws 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:
      IOException - if any I/O error has occurred
    • url2file

      public static File url2file(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 URL file2url(File file) throws 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:
      MalformedURLException - if file can't be represented as URL for some reason