Class FileUtils


  • public class FileUtils
    extends java.lang.Object
    File Utilities
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int EMPTY_DIRECTORIES_ONLY
      Option to only delete empty directories.
      static int IGNORE_ERRORS
      Option not to throw exceptions when a deletion finally doesn't succeed.
      static int NONE
      Option to delete given File
      static int RECURSIVE
      Option to recursively delete given File
      static int RETRY
      Option to retry deletion if not successful
      static int SKIP_MISSING
      Option to skip deletion if file doesn't exist
    • Constructor Summary

      Constructors 
      Constructor Description
      FileUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void createNewFile​(java.io.File f)
      Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
      static void createSymLink​(java.io.File path, java.lang.String target)
      Create a symbolic link
      static java.io.File createTempDir​(java.lang.String prefix, java.lang.String suffix, java.io.File dir)
      Create a temporary directory.
      static void delete​(java.io.File f)
      Delete file or empty folder
      static void delete​(java.io.File f, int options)
      Delete file or folder
      static void mkdir​(java.io.File d)
      Creates the directory named by this abstract pathname.
      static void mkdir​(java.io.File d, boolean skipExisting)
      Creates the directory named by this abstract pathname.
      static void mkdirs​(java.io.File d)
      Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
      static void mkdirs​(java.io.File d, boolean skipExisting)
      Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
      static java.lang.String readSymLink​(java.io.File path)  
      static java.lang.String relativize​(java.lang.String base, java.lang.String other)
      This will try and make a given path relative to another.
      static void rename​(java.io.File src, java.io.File dst)
      Rename a file or folder.
      • Methods inherited from class java.lang.Object

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

      • RECURSIVE

        public static final int RECURSIVE
        Option to recursively delete given File
        See Also:
        Constant Field Values
      • RETRY

        public static final int RETRY
        Option to retry deletion if not successful
        See Also:
        Constant Field Values
      • SKIP_MISSING

        public static final int SKIP_MISSING
        Option to skip deletion if file doesn't exist
        See Also:
        Constant Field Values
      • IGNORE_ERRORS

        public static final int IGNORE_ERRORS
        Option not to throw exceptions when a deletion finally doesn't succeed.
        Since:
        2.0
        See Also:
        Constant Field Values
      • EMPTY_DIRECTORIES_ONLY

        public static final int EMPTY_DIRECTORIES_ONLY
        Option to only delete empty directories. This option can be combined with RECURSIVE
        Since:
        3.0
        See Also:
        Constant Field Values
    • Constructor Detail

      • FileUtils

        public FileUtils()
    • Method Detail

      • delete

        public static void delete​(java.io.File f)
                           throws java.io.IOException
        Delete file or empty folder
        Parameters:
        f - File to be deleted
        Throws:
        java.io.IOException - if deletion of f fails. This may occur if f didn't exist when the method was called. This can therefore cause IOExceptions during race conditions when multiple concurrent threads all try to delete the same file.
      • delete

        public static void delete​(java.io.File f,
                                  int options)
                           throws java.io.IOException
        Delete file or folder
        Parameters:
        f - File to be deleted
        options - deletion options, RECURSIVE for recursive deletion of a subtree, RETRY to retry when deletion failed. Retrying may help if the underlying file system doesn't allow deletion of files being read by another thread.
        Throws:
        java.io.IOException - if deletion of f fails. This may occur if f didn't exist when the method was called. This can therefore cause IOExceptions during race conditions when multiple concurrent threads all try to delete the same file. This exception is not thrown when IGNORE_ERRORS is set.
      • rename

        public static void rename​(java.io.File src,
                                  java.io.File dst)
                           throws java.io.IOException
        Rename a file or folder. If the rename fails and if we are running on a filesystem where it makes sense to repeat a failing rename then repeat the rename operation up to 9 times with 100ms sleep time between two calls. Furthermore if the destination exists and is directory hierarchy with only directories in it, the whole directory hierarchy will be deleted. If the target represents a non-empty directory structure, empty subdirectories within that structure may or may not be deleted even if the method fails. Furthermore if the destination exists and is a file then the file will be deleted and then the rename is retried.

        This operation is not atomic.

        Parameters:
        src - the old File
        dst - the new File
        Throws:
        java.io.IOException - if the rename has failed
        Since:
        3.0
        See Also:
        FS.retryFailedLockFileCommit()
      • mkdir

        public static void mkdir​(java.io.File d)
                          throws java.io.IOException
        Creates the directory named by this abstract pathname.
        Parameters:
        d - directory to be created
        Throws:
        java.io.IOException - if creation of d fails. This may occur if d did exist when the method was called. This can therefore cause IOExceptions during race conditions when multiple concurrent threads all try to create the same directory.
      • mkdir

        public static void mkdir​(java.io.File d,
                                 boolean skipExisting)
                          throws java.io.IOException
        Creates the directory named by this abstract pathname.
        Parameters:
        d - directory to be created
        skipExisting - if true skip creation of the given directory if it already exists in the file system
        Throws:
        java.io.IOException - if creation of d fails. This may occur if d did exist when the method was called. This can therefore cause IOExceptions during race conditions when multiple concurrent threads all try to create the same directory.
      • mkdirs

        public static void mkdirs​(java.io.File d)
                           throws java.io.IOException
        Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.
        Parameters:
        d - directory to be created
        Throws:
        java.io.IOException - if creation of d fails. This may occur if d did exist when the method was called. This can therefore cause IOExceptions during race conditions when multiple concurrent threads all try to create the same directory.
      • mkdirs

        public static void mkdirs​(java.io.File d,
                                  boolean skipExisting)
                           throws java.io.IOException
        Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.
        Parameters:
        d - directory to be created
        skipExisting - if true skip creation of the given directory if it already exists in the file system
        Throws:
        java.io.IOException - if creation of d fails. This may occur if d did exist when the method was called. This can therefore cause IOExceptions during race conditions when multiple concurrent threads all try to create the same directory.
      • createNewFile

        public static void createNewFile​(java.io.File f)
                                  throws java.io.IOException
        Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

        Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.

        Parameters:
        f - the file to be created
        Throws:
        java.io.IOException - if the named file already exists or if an I/O error occurred
      • createSymLink

        public static void createSymLink​(java.io.File path,
                                         java.lang.String target)
                                  throws java.io.IOException
        Create a symbolic link
        Parameters:
        path -
        target -
        Throws:
        java.io.IOException
        Since:
        3.0
      • readSymLink

        public static java.lang.String readSymLink​(java.io.File path)
                                            throws java.io.IOException
        Parameters:
        path -
        Returns:
        the target of the symbolic link, or null if it is not a symbolic link
        Throws:
        java.io.IOException
        Since:
        3.0
      • createTempDir

        public static java.io.File createTempDir​(java.lang.String prefix,
                                                 java.lang.String suffix,
                                                 java.io.File dir)
                                          throws java.io.IOException
        Create a temporary directory.
        Parameters:
        prefix -
        suffix -
        dir - The parent dir, can be null to use system default temp dir.
        Returns:
        the temp dir created.
        Throws:
        java.io.IOException
        Since:
        3.4
      • relativize

        public static java.lang.String relativize​(java.lang.String base,
                                                  java.lang.String other)
        This will try and make a given path relative to another.

        For example, if this is called with the two following paths :

         base = "c:\\Users\\jdoe\\eclipse\\git\\project"
         other = "c:\\Users\\jdoe\\eclipse\\git\\another_project\\pom.xml"
         
        This will return "..\\another_project\\pom.xml".

        This method uses File.separator to split the paths into segments.

        Note that this will return the empty String if base and other are equal.

        Parameters:
        base - The path against which other should be relativized. This will be assumed to denote the path to a folder and not a file.
        other - The path that will be made relative to base.
        Returns:
        A relative path that, when resolved against base, will yield the original other.
        Since:
        3.7