Class FileOpUtils


  • public final class FileOpUtils
    extends java.lang.Object
    Some convenience methods for working with Files/FileOps.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.io.File append​(java.io.File base, java.lang.String... segments)
      Appends the given segments to the base file.
      static java.io.File append​(java.lang.String base, java.lang.String... segments)
      Appends the given segments to the base file.
      static FileOp create()
      Deprecated.
      Use Paths, CancellableFileIo and (for testing) InMemoryFileSystems directly.
      static FileOp create​(java.nio.file.Path path)
      Deprecated.
      Use Paths, CancellableFileIo and (for testing) InMemoryFileSystems directly.
      static boolean deleteFileOrFolder​(java.nio.file.Path fileOrFolder)
      Helper to delete a file or a directory.
      static boolean deleteIfExists​(java.io.File file, FileOp fop)
      Deprecated.
      Use Files.deleteIfExists(Path).
      static java.nio.file.Path getNewTempDir​(java.lang.String base, FileOp fileOp)
      static java.nio.file.Path getNewTempDir​(java.lang.String base, java.nio.file.FileSystem fileSystem)  
      static boolean isWindows()  
      static java.nio.file.Path[] listFiles​(java.nio.file.Path file)
      Invokes CancellableFileIo.list(Path) on the given file.
      static java.lang.String makeRelative​(java.io.File baseDir, java.io.File toBeRelative)
      Computes a relative path from "toBeRelative" relative to "baseDir".
      static void recursiveCopy​(java.io.File src, java.io.File dest, FileOp fop, ProgressIndicator progress)
      static void recursiveCopy​(java.nio.file.Path src, java.nio.file.Path dest, boolean merge, ProgressIndicator progress)  
      static void safeRecursiveOverwrite​(java.nio.file.Path src, java.nio.file.Path dest, ProgressIndicator progress)
      Moves a file or directory from one location to another.
      static void setExecutablePermission​(java.nio.file.Path path)  
      static java.io.File toFile​(java.nio.file.Path path)
      Convenience method to allow Path-based code to interop with legacy File-based code.
      static java.io.File toFileUnsafe​(java.nio.file.Path path)
      Temporary functionality to help with File-to-Path migration.
      • Methods inherited from class java.lang.Object

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

      • create

        @NonNull
        public static FileOp create()
        Deprecated.
        Use Paths, CancellableFileIo and (for testing) InMemoryFileSystems directly.
        The standard way to create a FileOp that interacts with the real filesystem.
      • create

        @NonNull
        public static FileOp create​(@Nullable
                                    java.nio.file.Path path)
        Deprecated.
        Use Paths, CancellableFileIo and (for testing) InMemoryFileSystems directly.
        Create a FileOp that supports the provided path. This should be used where code using only Paths directly has to interact with code that still uses FileOp.
      • recursiveCopy

        public static void recursiveCopy​(@NonNull
                                         java.io.File src,
                                         @NonNull
                                         java.io.File dest,
                                         @NonNull
                                         FileOp fop,
                                         @NonNull
                                         ProgressIndicator progress)
                                  throws java.io.IOException
        Copies a file or directory tree to the given location. dest should not exist: with the file system currently looking like
        
         /
           dir1/
             a.txt
           dir2/
        
         
        Running recursiveCopy(new File("/dir1"), new File("/dir2"), fOp) will result in an exception, while recursiveCopy(new File("/dir1"), new File("/dir2/foo") will result in
        
         /
           dir1/
             a.txt
           dir2/
             foo/
               a.txt
        
         
        This is equivalent to the behavior of cp -r when the target does not exist.
        Parameters:
        src - File to copy
        dest - Destination.
        fop - The FileOp to use for file operations.
        Throws:
        java.io.IOException - If the destination already exists, or if there is a problem copying the files or creating directories.
      • recursiveCopy

        public static void recursiveCopy​(@NonNull
                                         java.nio.file.Path src,
                                         @NonNull
                                         java.nio.file.Path dest,
                                         boolean merge,
                                         @NonNull
                                         ProgressIndicator progress)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • listFiles

        @NonNull
        public static java.nio.file.Path[] listFiles​(@NonNull
                                                     java.nio.file.Path file)
        Invokes CancellableFileIo.list(Path) on the given file. Contrary to the Java API, this returns an empty array instead of null when the directory does not exist.
      • safeRecursiveOverwrite

        public static void safeRecursiveOverwrite​(@NonNull
                                                  java.nio.file.Path src,
                                                  @NonNull
                                                  java.nio.file.Path dest,
                                                  @NonNull
                                                  ProgressIndicator progress)
                                           throws java.io.IOException
        Moves a file or directory from one location to another. If the destination exists, it is moved away, and once the operation has completed successfully, deleted. If there is a problem during the copy, the original files are moved back into place.
        Parameters:
        src - File to move
        dest - Destination. Follows the same rules as recursiveCopy(File, File, FileOp, ProgressIndicator)}.
        progress - Currently only used for error logging.
        Throws:
        java.io.IOException - If some problem occurs during copies or directory creation.
      • getNewTempDir

        @Nullable
        public static java.nio.file.Path getNewTempDir​(@NonNull
                                                       java.lang.String base,
                                                       @NonNull
                                                       FileOp fileOp)
        Creates a new subdirectory of the system temp directory. The directory will be named <base> + NN, where NN makes the directory distinct from any existing directories.
      • getNewTempDir

        @Nullable
        public static java.nio.file.Path getNewTempDir​(@NonNull
                                                       java.lang.String base,
                                                       @NonNull
                                                       java.nio.file.FileSystem fileSystem)
      • append

        @NonNull
        public static java.io.File append​(@NonNull
                                          java.io.File base,
                                          @NonNull
                                          java.lang.String... segments)
        Appends the given segments to the base file.
        Parameters:
        base - A base file, non-null.
        segments - Individual folder or filename segments to append to the base file.
        Returns:
        A new file representing the concatenation of the base path with all the segments.
      • append

        @NonNull
        public static java.io.File append​(@NonNull
                                          java.lang.String base,
                                          @NonNull
                                          java.lang.String... segments)
        Appends the given segments to the base file.
        Parameters:
        base - A base file path, non-empty and non-null.
        segments - Individual folder or filename segments to append to the base path.
        Returns:
        A new file representing the concatenation of the base path with all the segments.
      • makeRelative

        @NonNull
        public static java.lang.String makeRelative​(@NonNull
                                                    java.io.File baseDir,
                                                    @NonNull
                                                    java.io.File toBeRelative)
                                             throws java.io.IOException
        Computes a relative path from "toBeRelative" relative to "baseDir".

        Rule: - let relative2 = makeRelative(path1, path2) - then pathJoin(path1 + relative2) == path2 after canonicalization.

        Principle: - let base = /c1/c2.../cN/a1/a2../aN - let toBeRelative = /c1/c2.../cN/b1/b2../bN - result is removes the common paths, goes back from aN to cN then to bN: - result = ../..../../1/b2../bN

        Parameters:
        baseDir - The base directory to be relative to.
        toBeRelative - The file or directory to make relative to the base.
        Returns:
        A path that makes toBeRelative relative to baseDir.
        Throws:
        java.io.IOException - If drive letters don't match on Windows or path canonicalization fails.
      • deleteIfExists

        public static boolean deleteIfExists​(java.io.File file,
                                             FileOp fop)
        Deprecated.
        Use Files.deleteIfExists(Path).
        Deletes the given file if it exists. Does nothing and returns successfully if the file didn't exist to begin with.
        Returns:
        true if the file no longer exists, false if we failed to delete it
      • isWindows

        public static boolean isWindows()
      • deleteFileOrFolder

        public static boolean deleteFileOrFolder​(@NonNull
                                                 java.nio.file.Path fileOrFolder)
        Helper to delete a file or a directory. For a directory, recursively deletes all of its content. It's ok for the file or folder to not exist at all.
        Returns:
        true if the delete was successful
      • toFileUnsafe

        @NonNull
        public static java.io.File toFileUnsafe​(@NonNull
                                                java.nio.file.Path path)
        Temporary functionality to help with File-to-Path migration. Should only be called when a FileOp is not available and with a Path that is backed by the default FileSystem (notably not in the context of any tests that use MockFileOp or jimfs).
        Throws:
        java.lang.UnsupportedOperationException - if the Path is backed by a non-default FileSystem.
      • setExecutablePermission

        public static void setExecutablePermission​(@NonNull
                                                   java.nio.file.Path path)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • toFile

        @NonNull
        public static java.io.File toFile​(@NonNull
                                          java.nio.file.Path path)
        Convenience method to allow Path-based code to interop with legacy File-based code.

        Note that the resulting File may not refer to an actual file on the default FileSystem.