Class FileUtil


  • public final class FileUtil
    extends Object
    • Method Detail

      • createObjectTempDir

        public static Path createObjectTempDir​(Path parent,
                                               String objectId)
        Creates a new directory as a child of the parent path named: md5(objectId)-[random-long]

        A hash is used to avoid problems with object ids that are longer than 255 characters.

        Parameters:
        parent - the path to create the new directory under
        objectId - the object id to create the directory for
        Returns:
        the path to the new directory
      • moveDirectory

        public static void moveDirectory​(Path srcRoot,
                                         Path dstRoot)
                                  throws FileAlreadyExistsException
        Performs an atomic directory move. The srcRoot must exist and the dstRoot must NOT exist.

        First, an atomic move (rename) is attempted. If this fails, because the source and destination are on different volumes, then the destination directory is created and the source files are recursively moved to the destination.

        Parameters:
        srcRoot - source directory to move, most exist
        dstRoot - destination directory, must NOT exist but its parent must exist
        Throws:
        FileAlreadyExistsException - when the move fails because the destination already exists
      • deleteChildren

        public static void deleteChildren​(Path root)
        Recursively deletes all of the children of the specified path. The root path itself is not deleted.
        Parameters:
        root - the path under which all children should be deleted
      • deleteEmptyDirs

        public static void deleteEmptyDirs​(Path root)
        Deletes any empty directories under the specified path
        Parameters:
        root - the path to prune empty child directories from
      • deleteDirAndParentsIfEmpty

        public static void deleteDirAndParentsIfEmpty​(Path path)
        Deletes the specified directory and any of its parents that are empty. The search terminates as soon as a non-empty parent directory is encountered.
        Parameters:
        path - the path do delete
      • deleteDirAndParentsIfEmpty

        public static void deleteDirAndParentsIfEmpty​(Path path,
                                                      Path stop)
        Deletes the specified path and all of its ancestors until the stop directory is reached
        Parameters:
        path - directory to delete
        stop - directory to stop at
      • deleteDirIfEmpty

        public static boolean deleteDirIfEmpty​(Path directory)
        Attempts to delete the specified directory and ignores failures related to the directory not existing or not empty
        Parameters:
        directory - directory to delete
        Returns:
        true if the directory was deleted
      • safeDeleteDirectory

        public static void safeDeleteDirectory​(Path path)
        Deletes the path and all of its children. Any exception that is thrown is logged as a warning.
        Parameters:
        path - the path to delete
      • safeDelete

        public static void safeDelete​(Path path)
        Non-recursive path delete. Any exception is swallowed and logged.
        Parameters:
        path - the path to delete
      • deleteDirectory

        public static void deleteDirectory​(Path directory)
        Recursive delete that deletes everything under and including the specified directory. Failures deleting individual files are logged and a single exception is thrown at the end.
        Parameters:
        directory - the directory to delete
      • isDirEmpty

        public static boolean isDirEmpty​(Path path)
      • findFiles

        public static List<Path> findFiles​(Path path)
      • hasChildren

        public static boolean hasChildren​(Path path)
      • pathToStringStandardSeparator

        public static String pathToStringStandardSeparator​(Path path)
        Returns a string representation of a Path that uses '/' as the file separator.
        Parameters:
        path - the path to represent as a string
        Returns:
        path using '/' as the separator
      • pathJoinIgnoreEmpty

        public static String pathJoinIgnoreEmpty​(String... parts)
        Joins all of the parts together as a path separated by '/'. Leading and trailing slashes on path parts are normalized, but slashes within parts are not changed. Empty parts are ignored.
        Parameters:
        parts - the path parts to join
        Returns:
        joined path with empty elements left out
      • pathJoinFailEmpty

        public static String pathJoinFailEmpty​(String... parts)
        Joins all of the parts together as a path separated by '/'. Leading and trailing slashes on path parts are normalized, but slashes within parts are not changed. Throws an IllegalArgumentException if empty parts are encountered.
        Parameters:
        parts - the path parts to join
        Returns:
        joined path
      • parentPath

        public static String parentPath​(String path)
        Returns the path to the parent of the specified path, using forward slashes as path separators. If the path has no parent, then an empty string is returned.
        Parameters:
        path - path
        Returns:
        the path's parent