Class FileUtil


  • public class FileUtil
    extends java.lang.Object
    NIO file utility extensions.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void deleteRecursively​(java.nio.file.Path path)
      Delete the file or directory recursively.
      static java.nio.file.Path readCanonicalPath​(java.nio.file.Path path)
      Read and parse the path to its absolute canonical path.
      static void replaceSymbolicLink​(java.nio.file.Path link, java.nio.file.Path target)
      Similar to Files.createSymbolicLink(Path, Path, FileAttribute[]), but will replace the link if it already exists, and will try to write / replace it as an atomic operation.
      • Methods inherited from class java.lang.Object

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

      • readCanonicalPath

        public static java.nio.file.Path readCanonicalPath​(java.nio.file.Path path)
                                                    throws java.io.IOException
        Read and parse the path to its absolute canonical path.

        To circumvent the problem that java cached file metadata, including symlink targets, we need to read canonical paths directly. This includes resolving symlinks and relative path resolution (../..).

        This will read all the file meta each time and not use any of the java file meta caching, so will probably be a little slower. So should not be used repeatedly or too often.

        Parameters:
        path - The path to make canonical path of.
        Returns:
        The resolved canonical path.
        Throws:
        java.io.IOException - If unable to read the path.
      • replaceSymbolicLink

        public static void replaceSymbolicLink​(java.nio.file.Path link,
                                               java.nio.file.Path target)
                                        throws java.io.IOException
        Similar to Files.createSymbolicLink(Path, Path, FileAttribute[]), but will replace the link if it already exists, and will try to write / replace it as an atomic operation.
        Parameters:
        link - The path of the symbolic link. The parent directory of the link file must already exist, and must be writable. See Files.createDirectories(Path, FileAttribute[])
        target - The target path.
        Throws:
        java.io.IOException - If unable to create symbolic link.
      • deleteRecursively

        public static void deleteRecursively​(java.nio.file.Path path)
                                      throws java.io.IOException
        Delete the file or directory recursively.
        Parameters:
        path - The file or directory path.
        Throws:
        java.io.IOException - If delete failed at some point.