Class FileUtil

java.lang.Object
org.omegat.util.FileUtil

public final class FileUtil extends Object
Files processing utilities.
  • Field Details

  • Method Details

    • getRecentBackup

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

      public static void removeOldBackups(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 File backupFile(File original)
      Create file backup with datetime suffix.
      Parameters:
      original - a file to copy as backup file.
      Returns:
      Backup file.
    • getBackupFilename

      public static String getBackupFilename(File original)
      Generates a name for the file to be backuped, in the form of [original_name].yyyyMMddHHmm.bak.
      Parameters:
      original - the file to be backuped
      Returns:
      the name of the backuped file
    • rename

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

      public static void copyFileWithEolConversion(File inFile, File outFile, Charset charset) throws 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:
      IOException
    • getEOL

      public static String getEOL(File file, Charset charset) throws 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:
      IOException
    • findFiles

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

      public static List<File> findFiles(File dir, FileFilter filter, Comparator<File> comp)
    • computeRelativePath

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

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

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

      public static void copyFilesTo(File destination, File[] toCopy, FileUtil.ICollisionCallback onCollision) throws 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:
      IOException
    • isRelative

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

      public static String absoluteForSystem(String path)
      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 List<File> buildFileList(File rootDir, boolean recursive) throws IOException
      Returns a list of all files under the root directory by absolute path.
      Throws:
      IOException
    • buildRelativeFilesList

      public static List<String> buildRelativeFilesList(File rootDir, List<String> includes, List<String> excludes) throws IOException
      Throws:
      IOException
    • checkFileInclude

      public static boolean checkFileInclude(String filePath, Pattern[] includes, Pattern[] excludes)
    • compileFileMasks

      public static Pattern[] compileFileMasks(List<String> masks)
    • getUniqueNames

      public static List<String> getUniqueNames(List<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