Class FileUtil


  • public final class FileUtil
    extends java.lang.Object
    Files processing utilities.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  FileUtil.ICollisionCallback  
      static class  FileUtil.TmFileComparator
      Comparator to sort the tm/ folder alphabetically, but always put tm/enforce and tm/auto results before other similar % matches.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String absoluteForSystem​(java.lang.String path, Platform.OsType currentOsType)
      Converts Windows absolute path into current system's absolute path.
      static java.io.File backupFile​(java.io.File original)
      Create file backup with datetime suffix.
      static java.util.List<java.io.File> buildFileList​(java.io.File rootDir, boolean recursive)
      Returns a list of all files under the root directory by absolute path.
      static java.util.List<java.lang.String> buildRelativeFilesList​(java.io.File rootDir, java.util.List<java.lang.String> includes, java.util.List<java.lang.String> excludes)  
      static boolean checkFileInclude​(java.lang.String filePath, java.util.regex.Pattern[] includes, java.util.regex.Pattern[] excludes)  
      static java.util.regex.Pattern[] compileFileMasks​(java.util.List<java.lang.String> masks)  
      static java.lang.String computeRelativePath​(java.io.File rootDir, java.io.File file)
      Compute relative path of file.
      static void copyFilesTo​(java.io.File destination, java.io.File[] toCopy, FileUtil.ICollisionCallback onCollision)
      Copy a collection of files to a destination.
      static void copyFileWithEolConversion​(java.io.File inFile, java.io.File outFile, java.nio.charset.Charset charset)
      Copy file and create output directory if need.
      static java.lang.String expandTildeHomeDir​(java.lang.String path)
      Expand tilde(~) in first character in path string.
      static java.util.List<java.io.File> findFiles​(java.io.File dir, java.io.FileFilter filter)
      Find files in subdirectories.
      static java.lang.String getEOL​(java.io.File file, java.nio.charset.Charset charset)
      Read a file to determine its end-of-line character(s).
      static java.io.File getRecentBackup​(java.io.File originalFile)
      Get most recent backup file path.
      static java.util.List<java.lang.String> getUniqueNames​(java.util.List<java.lang.String> paths)
      Given a list of paths, return a list of filenames (a la File.getName()) plus the minimum number of parent path segments required to make each filename unique within the result list.
      static boolean isInPath​(java.io.File path, java.io.File tmxFile)
      Check if file is in specified path.
      static boolean isRelative​(java.lang.String path)
      Checks if path starts with possible root on the Linux, MacOS, Windows.
      static void removeOldBackups​(java.io.File originalFile, int maxBackups)
      Removes older backups to be under specified number of files.
      static void rename​(java.io.File from, java.io.File to)
      Renames file, with checking errors and 3 seconds retry against external programs (like antivirus or TortoiseSVN) locking.
      • Methods inherited from class java.lang.Object

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

      • getRecentBackup

        public static java.io.File getRecentBackup​(java.io.File originalFile)
        Get most recent backup file path.
        Parameters:
        originalFile - target original file name.
      • removeOldBackups

        public static void removeOldBackups​(java.io.File originalFile,
                                            int maxBackups)
        Removes older backups to be under specified number of files.
        Parameters:
        originalFile - target original file name.
        maxBackups - maximum number of back up files.
      • backupFile

        public static java.io.File backupFile​(java.io.File original)
        Create file backup with datetime suffix.
        Parameters:
        original - a file to copy as backup file.
        Returns:
        Backup file.
      • rename

        public static void rename​(java.io.File from,
                                  java.io.File to)
                           throws java.io.IOException
        Renames file, with checking errors and 3 seconds retry against external programs (like antivirus or TortoiseSVN) locking.
        Throws:
        java.io.IOException
      • copyFileWithEolConversion

        public static void copyFileWithEolConversion​(java.io.File inFile,
                                                     java.io.File outFile,
                                                     java.nio.charset.Charset charset)
                                              throws java.io.IOException
        Copy file and create output directory if need. EOL will be converted into target-specific or into platform-specific if target doesn't exist.
        Throws:
        java.io.IOException
      • getEOL

        public static java.lang.String getEOL​(java.io.File file,
                                              java.nio.charset.Charset charset)
                                       throws java.io.IOException
        Read a file to determine its end-of-line character(s). If neither '\n' nor '\r' are present in the file then it will return null.
        Parameters:
        file -
        charset -
        Returns:
        The EOL character(s) as a string, or null if not detectable
        Throws:
        java.io.IOException
      • findFiles

        public static java.util.List<java.io.File> findFiles​(java.io.File dir,
                                                             java.io.FileFilter filter)
        Find files in subdirectories.
        Parameters:
        dir - directory to start find
        filter - filter for found files
        Returns:
        list of filtered found files
      • computeRelativePath

        public static java.lang.String computeRelativePath​(java.io.File rootDir,
                                                           java.io.File file)
                                                    throws java.io.IOException
        Compute relative path of file.
        Parameters:
        rootDir - root directory
        file - file
        Returns:
        Throws:
        java.io.IOException
      • isInPath

        public static boolean isInPath​(java.io.File path,
                                       java.io.File tmxFile)
        Check if file is in specified path.
      • expandTildeHomeDir

        public static java.lang.String expandTildeHomeDir​(java.lang.String path)
        Expand tilde(~) in first character in path string.
        Parameters:
        path - path string
        Returns:
        expanded path string
      • copyFilesTo

        public static void copyFilesTo​(java.io.File destination,
                                       java.io.File[] toCopy,
                                       FileUtil.ICollisionCallback onCollision)
                                throws java.io.IOException
        Copy a collection of files to a destination. Recursively copies contents of directories while preserving relative paths. Provide an FileUtil.ICollisionCallback to determine what to do with files with conflicting names; they will be overwritten if the callback is null.
        Parameters:
        destination - Directory to copy to
        toCopy - Files to copy
        onCollision - Callback that determines what to do in case files with the same name already exist
        Throws:
        java.io.IOException
      • isRelative

        public static boolean isRelative​(java.lang.String path)
        Checks if path starts with possible root on the Linux, MacOS, Windows.
      • absoluteForSystem

        public static java.lang.String absoluteForSystem​(java.lang.String path,
                                                         Platform.OsType currentOsType)
        Converts Windows absolute path into current system's absolute path. It required for conversion like 'C:\zzz' into '/zzz' for be real absolute in Linux.
      • buildFileList

        public static java.util.List<java.io.File> buildFileList​(java.io.File rootDir,
                                                                 boolean recursive)
                                                          throws java.io.IOException
        Returns a list of all files under the root directory by absolute path.
        Throws:
        java.io.IOException
      • buildRelativeFilesList

        public static java.util.List<java.lang.String> buildRelativeFilesList​(java.io.File rootDir,
                                                                              java.util.List<java.lang.String> includes,
                                                                              java.util.List<java.lang.String> excludes)
                                                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • checkFileInclude

        public static boolean checkFileInclude​(java.lang.String filePath,
                                               java.util.regex.Pattern[] includes,
                                               java.util.regex.Pattern[] excludes)
      • compileFileMasks

        public static java.util.regex.Pattern[] compileFileMasks​(java.util.List<java.lang.String> masks)
      • getUniqueNames

        public static java.util.List<java.lang.String> getUniqueNames​(java.util.List<java.lang.String> paths)
        Given a list of paths, return a list of filenames (a la File.getName()) plus the minimum number of parent path segments required to make each filename unique within the result list. E.g.
        • [foo/bar.txt, foo/baz.txt] -> [bar.txt, baz.txt]
        • [foo/bar/baz.txt, foo/fop/baz.txt] -> [bar/baz.txt, fop/baz.txt]
        • [foo/bar/baz/fop.txt, foo/buz/baz/fop.txt] -> [bar/baz/fop.txt, buz/baz/fop.txt]
        • [foo.txt, foo.txt] -> [foo.txt, foo.txt] (actual duplicates are unmodified)
        Note that paths will be normalized (indirections removed, trailing and duplicate separators removed, separators become /).
        Parameters:
        paths - A list of paths
        Returns:
        A list of minimal unique paths