Class FileUtil

java.lang.Object
org.apache.uima.pear.util.FileUtil

public class FileUtil extends Object
The FileUtil class provides utility methods for working with general files.
  • Constructor Details

    • FileUtil

      public FileUtil()
  • Method Details

    • cleanUpDirectoryContent

      public static int cleanUpDirectoryContent(File directory) throws IOException
      Deletes all files and subdirectories in a given directory. In case of unsuccessful deletion, calls the deleteOnExit() method to request that files and subdirs are deleted when the JVM terminates.
      Parameters:
      directory - The given directory to be cleaned-up.
      Returns:
      The number of successfully deleted entries in the given directory.
      Throws:
      IOException - If an I/O exception occurred.
    • cleanUpDirectoryFiles

      public static int cleanUpDirectoryFiles(File directory) throws IOException
      Deletes all files in a given directory. In case of unsuccessful deletion, calls the deleteOnExit() method to request that files are deleted when the JVM terminates.
      Parameters:
      directory - The given directory to be cleaned-up.
      Returns:
      The number of successfully deleted entries in the given directory.
      Throws:
      IOException - If an I/O exception occurred.
    • cleanUpDirectoryFiles

      public static int cleanUpDirectoryFiles(File directory, int maxLimit) throws IOException
      Cleans-up a given directory by keeping the number of files within a given limit. Deletes the oldest files first. In case of unsuccessful deletion, calls the deleteOnExit() method to request that files are deleted when the JVM terminates.
      Parameters:
      directory - The given directory.
      maxLimit - The given maximum limit of the number of files in the given directory.
      Returns:
      The number of actually deleted files.
      Throws:
      IOException - If an I/O exception occurred.
    • computeRelativePath

      public static String computeRelativePath(File referenceDir, File file) throws IOException
      Computes relative path to a given file from a given reference directory, if both the reference directory and the file are in the same logical file system (partition).
      Parameters:
      referenceDir - The given reference directory.
      file - The given file.
      Returns:
      The relative path to the given file from the given reference directory, or null, if the relative path does not exist.
      Throws:
      IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries.
    • copyFile

      @Deprecated public static boolean copyFile(File source, File destination) throws IOException
      Deprecated.
      Copies the content of a given source file to a given destination file.
      Parameters:
      source - The given source file.
      destination - The given destination file.
      Returns:
      true if the copy operation completed successfully, false otherwise.
      Throws:
      IOException - If any I/O exception occurred.
    • copyFile

      @Deprecated public static boolean copyFile(URL sourceUrl, File destination) throws IOException
      Deprecated.
      Copies the content of a given remote source file to a given destination file.
      Parameters:
      sourceUrl - The given source file URL.
      destination - The given destination file.
      Returns:
      true if the copy operation completed successfully, false otherwise.
      Throws:
      IOException - If any I/O exception occurred.
    • createDirList

      public static Collection<File> createDirList(File rootDir) throws IOException
      Creates list of subdirectories in a given root directory, including all its subdirectories.
      Parameters:
      rootDir - The given root directory.
      Returns:
      Collection of File objects, representing subdirectories in the given root directory and all its subdirectories.
      Throws:
      IOException - If any I/O exception occurs.
    • createDirList

      public static Collection<File> createDirList(File rootDir, boolean includeSubdirs) throws IOException
      Creates list of subdirectories in a given root directory. If a given boolean flag is true, all the subdirectories of the given root directory are also scanned, otherwise only subdirectories in the given root directory are included.
      Parameters:
      rootDir - The given root directory.
      includeSubdirs - If true, the returned list includes sub-directories from all sub-directories of the given root directory, otherwise it includes only sub-directories from the given root directory itself.
      Returns:
      Collection of File objects, representing subdirectories in the given root directory.
      Throws:
      IOException - If any I/O exception occurs.
    • createDirList

      public static Collection<File> createDirList(JarFile archive) throws IOException
      Creates a list of directories in a given archive (JAR) file. The root directory path, used to represent the directories, is set to the input archive file path without the file name extension.
      Parameters:
      archive - The input archive (JAR) file.
      Returns:
      Collection of File objects, representing directories in the given archive file.
      Throws:
      IOException - If any I/O exception occurs.
    • createFileList

      public static Collection<File> createFileList(File filesDir) throws IOException
      Creates list of files in a given directory, including all its subdirectories.
      Parameters:
      filesDir - The given directory.
      Returns:
      Collection of File objects in the given directory, including all its subdirectories.
      Throws:
      IOException - If any I/O exception occurs.
    • createFileList

      public static Collection<File> createFileList(File filesDir, boolean includeSubdirs) throws IOException
      Creates list of files in a given directory. If a given boolean flag is true, all the sub-directories of the given directory are also scanned, otherwise only files in the given directory are included.
      Parameters:
      filesDir - The given directory.
      includeSubdirs - If true, the returned file list includes files from all the sub-directories of the given directory, otherwise it includes only files from the given directory itself.
      Returns:
      Collection of File objects in the given directory.
      Throws:
      IOException - If any I/O exception occurs.
    • createFileList

      public static Collection<File> createFileList(JarFile archive) throws IOException
      Creates a list of files in a given archive (JAR) file. The root directory path, used to represent the files, is set to the input archive file path without the file name extension.
      Parameters:
      archive - The input archive (JAR) file.
      Returns:
      Collection of File objects, representing files in the given archive file.
      Throws:
      IOException - If any I/O exception occurs.
    • createTempFile

      @Deprecated public static File createTempFile(String prefix, String suffix) throws IOException
      Deprecated.
      use Java 7 method for this see File.createTempFile(String, String, File)
      Creates a new empty file in a directory specified by the 'java.io.tmpdir' or the 'user.home' system property, using given prefix and suffix strings to generate its name. For more information see the documentation on the java.io.File.createTempFile() method.
      Parameters:
      prefix - The given prefix string to be used in generating the file's name; must be at least three characters long.
      suffix - The given suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used.
      Returns:
      The File object denoting the newly created file.
      Throws:
      IOException - If a temporary directory not found or other I/O exception occurred.
    • deleteDirectory

      public static boolean deleteDirectory(File dir) throws IOException
      Deletes a given directory, including all its subdirectories and files. Returns true if the deletion was successful, otherwise returns false. In case of unsuccessful deletion, calls deleteOnExit() method to request that files and subdirs be deleted when the virtual machine terminates.
      Parameters:
      dir - The given directory to be deleted.
      Returns:
      true if the deletion was successful, otherwise false.
      Throws:
      IOException - If any I/O exception occurs.
    • extractDirectoryFromJar

      public static long extractDirectoryFromJar(JarFile jarFile, String dirPath, File targetDir) throws IOException
      Extracts all files in a given JAR directory (including all its subdirectories) from a given JAR file to a given target directory.
      Parameters:
      jarFile - The given JAR file.
      dirPath - The given JAR directory.
      targetDir - The given target directory.
      Returns:
      Total number of bytes extracted.
      Throws:
      IOException - If any I/O exception occurred.
    • extractFilesWithExtFromJar

      public static long extractFilesWithExtFromJar(JarFile jarFile, String fileExt, File targetDir) throws IOException
      Extracts all files that have a given extension from a given JAR file to a given target directory. To extract files without extension, use null as the fileExt parameter.
      Parameters:
      jarFile - The given JAR file.
      fileExt - The given file extension.
      targetDir - The given target directory.
      Returns:
      Total number of bytes extracted.
      Throws:
      IOException - If any I/O exception occurred.
    • extractFilesFromJar

      public static long extractFilesFromJar(JarFile jarFile, File targetDir) throws IOException
      Extracts all files from a given JAR file to a given target directory.
      Parameters:
      jarFile - The given JAR file.
      targetDir - The given target directory.
      Returns:
      Total number of bytes extracted.
      Throws:
      IOException - If any I/O exception occurred.
    • extractFilesFromJar

      public static long extractFilesFromJar(JarFile jarFile, File targetDir, FileFilter filter) throws IOException
      Extracts files from a given JAR file to a given target directory, based on a given FileFilter object.
      Parameters:
      jarFile - The given JAR file.
      targetDir - The given target directory.
      filter - The given FileFilter object.
      Returns:
      Total number of bytes extracted.
      Throws:
      IOException - If any I/O exception occurred.
    • getAbsolutePath

      public static String getAbsolutePath(File rootDir, String relativePath)
      Constructs an absolute path of a given object, located in a given root directory, based on its relative path in this directory.
      Parameters:
      rootDir - The given root directory.
      relativePath - The given relative path of the object.
      Returns:
      The absolute path for the given object, located in the given root directory.
    • getFileNameExtension

      public static String getFileNameExtension(String fileName)
      Identifies a given file name extension.
      Parameters:
      fileName - The given file name.
      Returns:
      The file name extension
    • getFileSize

      @Deprecated public static long getFileSize(String fileLocation)
      Deprecated.
      use Java 7 method for this see Files.size(Path)
      Returns file size for a given file.
      Parameters:
      fileLocation - The given file location - local file path or URL.
      Returns:
      The given file size, if the specified file can be accessed, -1 otherwise.
    • getRelativePath

      public static String getRelativePath(File rootDir, String absolutePath)
      Constructs a relative path of a given object, located in a given root directory, based on its absolute path.
      Parameters:
      rootDir - The given root directory.
      absolutePath - The given absolute path of the object.
      Returns:
      The relative path of the given object, located in the given root directory.
    • identifyUtfSignature

      public static String identifyUtfSignature(int[] prefix, int length)
      Makes and attempt to identify possible UTF signature (BOM) in a given sequence of bytes. Returns the identified UTF signature name or null, if the signature could not be identified. For more on UTF and its signatures see FAQ - UTF and BOM.
      Parameters:
      prefix - The given sequence of bytes to analyze.
      length - The length of the given sequence of bytes.
      Returns:
      The UTF signature name or null, if the signature could not be identified.
    • isAsciiFile

      public static boolean isAsciiFile(File textFile) throws IOException
      Returns true, if a given text file contains only ASCII characters, otherwise returns false.
      Parameters:
      textFile - The given text file.
      Returns:
      true, if the given text file contains only ASCII characters, false otherwise.
      Throws:
      IOException - If an I/O exception occurred.
    • isAsciiStream

      public static boolean isAsciiStream(InputStream iStream) throws IOException
      Returns true, if a given input stream contains only ASCII characters, otherwise returns false.
      Parameters:
      iStream - The given input stream.
      Returns:
      true, if the given input stream contains only ASCII characters, false otherwise.
      Throws:
      IOException - If an I/O exception occurred.
    • loadListOfStrings

      public static String[] loadListOfStrings(BufferedReader iStream) throws IOException
      Loads a list of non-empty EOL-delimited strings from a given text stream.
      Parameters:
      iStream - The given input text stream.
      Returns:
      The array of non-empty strings loaded from the given text stream.
      Throws:
      IOException - If any I/O exception occurred.
    • loadListOfStrings

      @Deprecated public static String[] loadListOfStrings(File textFile) throws IOException
      Deprecated.
      use Java 7 method for this see Files.readAllLines(Path, Charset)
      Loads a list of non-empty EOL-delimited strings from a given text file using the default file encoding.
      Parameters:
      textFile - The given text file.
      Returns:
      The array of non-empty strings loaded from the given text file.
      Throws:
      IOException - If any I/O exception occurred.
    • loadListOfStrings

      public static String[] loadListOfStrings(URL textFileURL) throws IOException
      Loads a list of non-empty EOL-delimited strings from a given remote text file.
      Parameters:
      textFileURL - The URL of the given input text file.
      Returns:
      The array of non-empty strings loaded from the given text file.
      Throws:
      IOException - If any I/O exception occurred.
    • loadPropertiesFromJar

      public static Properties loadPropertiesFromJar(String propFilePath, JarFile jarFile) throws IOException
      Loads a specified properties file from a given JAR file.
      Parameters:
      propFilePath - The given properties file path in the JAR file.
      jarFile - The given JAR file.
      Returns:
      Properties object containing loaded properties, or null, if the properties file was not found in the given JAR file.
      Throws:
      IOException - If any I/O exception occurred.
    • loadTextFile

      public static String loadTextFile(BufferedReader iStream) throws IOException
      Loads a text file associated with a given input stream.
      Parameters:
      iStream - The given text input stream.
      Returns:
      The content of the text file.
      Throws:
      IOException - If any I/O exception occurs.
    • loadTextFile

      @Deprecated public static String loadTextFile(File textFile) throws IOException
      Deprecated.
      use main file util for this, see FileUtils.file2String(File) if using the default charset is OK
      Loads a given local text file using the default file encoding.
      Parameters:
      textFile - The given text file.
      Returns:
      The content of the text file.
      Throws:
      IOException - If any I/O exception occurs.
    • loadTextFile

      @Deprecated public static String loadTextFile(File textFile, String encoding) throws IOException
      Deprecated.
      use main file util for this, see FileUtils.file2String(File, String) if using the default Charset is OK
      Loads a given local text file using a specified file encoding.
      Parameters:
      textFile - The given text file.
      encoding - The given text file encoding name.
      Returns:
      The content of the text file.
      Throws:
      IOException - If any I/O exception occurs.
    • loadTextFile

      public static String loadTextFile(URL textFileURL) throws IOException
      Loads a given remote text file.
      Parameters:
      textFileURL - The given text file URL.
      Returns:
      The content of the text file.
      Throws:
      IOException - If any I/O exception occurs.
    • loadTextFile

      public static String loadTextFile(URLConnection urlConnection) throws IOException
      Loads a given remote text file.
      Parameters:
      urlConnection - The given URL connection.
      Returns:
      The content of the text file.
      Throws:
      IOException - If any I/O exception occurs.
    • loadTextFileFromJar

      public static String loadTextFileFromJar(String filePath, JarFile jarFile) throws IOException
      Loads a specified text file from a given JAR file.
      Parameters:
      filePath - The specified text file path inside the JAR file.
      jarFile - The given JAR file.
      Returns:
      The content of the text specified file, or null, if the text file was not found in the given JAR file.
      Throws:
      IOException - If any I/O exception occurs.
    • localPathToFileUrl

      public static String localPathToFileUrl(String path)
      Converts a given input file path into a valid file URL string.
      Parameters:
      path - The given file path to be converted.
      Returns:
      The file URL string for the specified file.
    • moveFile

      @Deprecated public static boolean moveFile(File source, File destinationDir) throws IOException
      Deprecated.
      Moves a given source file to a given destination directory.
      Parameters:
      source - The given source file.
      destinationDir - The given destination directory.
      Returns:
      true if the move operation completed successfully, false otherwise.
      Throws:
      IOException - If any I/O exception occurred.
    • replaceStringInFile

      public static int replaceStringInFile(File textFile, String subStringRegex, String replacement) throws IOException
      Replaces all occurrences of a given regular expression with a given string in a given text file. Supports only 1 file encoding - ASCII - for all general text files. Supports 2 encodings - UTF-8 (ASCII) and UTF-16 for XML files.
      Parameters:
      textFile - The given text file.
      subStringRegex - The given regular expression string to be replaced.
      replacement - The given replacement string.
      Returns:
      The number of actual string replacements performed.
      Throws:
      IOException - If any I/O exception occurs.
    • sortFileListByTime

      public static SortedSet<File> sortFileListByTime(Collection<File> fileList)
      Sorts a given list of files by the 'last modified' time in the descending order.
      Parameters:
      fileList - The given list of files.
      Returns:
      The list of files sorted by the 'last modified' time in the descending order.
    • zipDirectory

      public static File zipDirectory(File dir2zip) throws IOException
      Zips the contents of a given directory. The output ZIP file, by default, is created in the given directory, and its name is the given directory name with 'zip' extension.
      Parameters:
      dir2zip - The given directory to be zipped.
      Returns:
      The output ZIP file.
      Throws:
      IOException - If any I/O exception occurred.
    • zipDirectory

      public static File zipDirectory(File dir2zip, File zippedFile) throws IOException
      Zips the contents of a given directory to a given output ZIP file.
      Parameters:
      dir2zip - The given directory to be zipped.
      zippedFile - The given output ZIP file.
      Returns:
      The output ZIP file.
      Throws:
      IOException - If any I/O exception occurred.
    • zipDirectory

      public static ZipOutputStream zipDirectory(File dir2zip, ZipOutputStream zoStream, File referenceDir, File[] excludeFiles) throws IOException
      Zips the contents of a given directory to a given ZIP output stream. Paths of file entries in the ZIP stream are taken relatively to a given reference directory. If the reference directory is null, the file paths are taken relatively to the given directory to be zipped. The method allows to specify the list of files (or dirs) that should not be zipped.
      Parameters:
      dir2zip - The given directory to be zipped.
      zoStream - The given ZIP output stream.
      referenceDir - The given reference directory or null.
      excludeFiles - The given list of files (or dirs) that should not be zipped.
      Returns:
      The ZIP output stream.
      Throws:
      IOException - If any I/O exception occurred.
    • zipFile

      public static File zipFile(File file2zip) throws IOException
      Zips a given file. The output ZIP file, by default, is created in the same directory, as the given input file, and has the same name, as the given input file with 'zip' extension.
      Parameters:
      file2zip - The file to be zipped.
      Returns:
      The output ZIP file.
      Throws:
      IOException - If any I/O exception occurred.
    • zipFile

      public static File zipFile(File file2zip, File zippedFile) throws IOException
      Zips a given file to a given output ZIP file.
      Parameters:
      file2zip - The file to be zipped.
      zippedFile - The given output ZIP file.
      Returns:
      The output ZIP file.
      Throws:
      IOException - If any I/O exception occurred.