Class FileUtils

java.lang.Object
org.apache.uima.util.FileUtils

public class FileUtils extends Object
Some utilities for handling files.
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • getFiles

      public static final ArrayList<File> getFiles(File directory, boolean getRecursive)
      Get a list of all files in a directory. Optionally, get files in subdirectories as well.
      Parameters:
      directory - The directory for which to get the files.
      getRecursive - Should we get files from subdirectories?
      Returns:
      ArrayList A list of File objects. null if directory does not exist, or is not a directory.
    • getFiles

      public static final ArrayList<File> getFiles(File directory)
      Get a list of all files in a directory, ignoring subdirectories.
      Parameters:
      directory - The directory for which to get the files.
      Returns:
      ArrayList A list of File objects. null if directory does not exist, or is not a directory.
    • getSubDirs

      public static final ArrayList<File> getSubDirs(File directory)
      Get a list of all subdirectories in a directory, ignoring regular files.
      Parameters:
      directory - The directory for which to get the subdirectories.
      Returns:
      ArrayList A list of File objects. null if directory does not exist, or is not a directory.
    • reader2String

      public static String reader2String(Reader reader) throws IOException
      Read a bufferedReader into a string, using the default platform encoding.
      Parameters:
      reader - to be read in
      Returns:
      String The contents of the stream.
      Throws:
      IOException - Various I/O errors.
    • file2String

      public static String file2String(File file) throws IOException
      Read the contents of a file into a string, using the default platform encoding.
      Parameters:
      file - The file to be read in.
      Returns:
      String The contents of the file.
      Throws:
      IOException - Various I/O errors.
    • file2String

      public static String file2String(File file, String fileEncoding) throws IOException
      Read the contents of a file into a string using a specific character encoding.
      Parameters:
      file - The input file.
      fileEncoding - The character encoding of the file (see your Java documentation for supported encodings).
      Returns:
      String The contents of the file.
      Throws:
      IOException - Various I/O errors.
    • saveString2File

      public static void saveString2File(String fileContents, File file) throws IOException
      Write a string to a file, using platform encoding. If the file exists, it is overwritten.
      Parameters:
      fileContents - The file contents.
      file - The file to save to.
      Throws:
      IOException - If for any reason the file can't be written.
    • saveString2File

      public static void saveString2File(String s, File file, String encoding) throws IOException
      Write a string to a file using the specified encoding. If the file exists, it is overwritten.
      Parameters:
      s - The file contents.
      file - The file to save to.
      encoding - The character encoding to use.
      Throws:
      IOException - If for any reason the file can't be written.
    • writeToFile

      public static void writeToFile(Path path, String data)
      Efficiently Writes string data as UTF-8 characters to path added for backwards compatibility with v2
      Parameters:
      path - where to write to, creating a new file if it doesn't already exist
      data - the data to write
    • deleteAllFiles

      public static final void deleteAllFiles(File directory)
      Delete all files in a directory (not recursive).
      Parameters:
      directory - The directory that contains the files to be deleted.
    • deleteRecursive

      public static final boolean deleteRecursive(File file)
      Recursively delete possibly non-empty directory or file.
      Parameters:
      file - The file or directory to be deleted.
      Returns:
      true iff the file/directory could be deleted.
    • mkdir

      public static final boolean mkdir(File directory)
      Create a new directory. The parent directory must exist.
      Parameters:
      directory - The directory to be created.
      Returns:
      boolean Will fail if directory already exists, or File.mkdir() returns false.
    • createTempDir

      @Deprecated public static final File createTempDir(File parent, String prefix)
      Deprecated.
      Create a temporary directory using random numbers as name suffixes.
      Parameters:
      parent - Where the directory is created.
      prefix - Prefix of the directory names to be created.
      Returns:
      A file object corresponding to the newly created dir, or null if none could be created for some reason (e.g., if the parent is not writable).
    • createTempFile

      @Deprecated public static final File createTempFile(String prefix, String suffix, File tempDir) throws IOException
      Deprecated.
      use Java 7 methods for this see File.createTempFile(String, String, File)
      Parameters:
      prefix - -
      suffix - -
      tempDir - -
      Returns:
      the file
      Throws:
      IOException - -
    • copyFile

      @Deprecated public static final void copyFile(File file, File dir) throws IOException
      Deprecated.
      Copy file file to location dir. This method will not fail silently. Anything that prevents the copy from happening will be treated as an error condition. If the method does not throw an exception, the copy was successful.

      Note: this is a completely brain-dead implementation of file copy. The complete file is read into memory before it is copied. If you need something more sophisticated for copying large files, feel free to add your improvements.

      Parameters:
      file - The file to copy.
      dir - The destination directory.
      Throws:
      IOException - For various reason: if file does not exist or is not readable, if the destination directory does not exist or isn't a directory, or if the file can't be copied for any reason.
    • findRelativePath

      public static String findRelativePath(File file, File relativeToDir) throws IOException
      Finds a relative path to a given file, relative to a specified directory.
      Parameters:
      file - file that the relative path should resolve to
      relativeToDir - directory that the path should be relative to
      Returns:
      a relative path. This always uses / as the separator character.
      Throws:
      IOException - -
    • getPathComponents

      public static String[] getPathComponents(String canonicalPath)
      Splits a path into components using the OS file separator character. This can be used on the results of File.getCanonicalPath().
      Parameters:
      canonicalPath - a file path that uses the OS file separator character
      Returns:
      an array of strings, one for each component of the path