org.apache.uima.util

## Class FileUtils

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

-     public class FileUtils
      extends Object

  Some utilities for handling files.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `FileUtils()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static void` | `copyFile(File file, File dir)` Copy file `file` to location `dir`.
    `static File` | `createTempDir(File parent, String prefix)` Create a temporary directory using random numbers as name suffixes.
    `static File` | `createTempFile(String prefix, String suffix, File tempDir)`
    `static void` | `deleteAllFiles(File directory)` Delete all files in a directory (not recursive).
    `static boolean` | `deleteRecursive(File file)` Recursively delete possibly non-empty directory or file.
    `static String` | `file2String(File file)` Read the contents of a file into a string, using the default platform encoding.
    `static String` | `file2String(File file, String fileEncoding)` Read the contents of a file into a string using a specific character encoding.
    `static String` | `findRelativePath(File file, File relativeToDir)` Finds a relative path to a given file, relative to a specified directory.
    `static ArrayList<File>` | `getFiles(File directory)` Get a list of all files in a directory, ignoring subdirectories.
    `static ArrayList<File>` | `getFiles(File directory, boolean getRecursive)` Get a list of all files in a directory.
    `static String[]` | `getPathComponents(String canonicalPath)` Splits a path into components using the OS file separator character.
    `static ArrayList<File>` | `getSubDirs(File directory)` Get a list of all subdirectories in a directory, ignoring regular files.
    `static boolean` | `mkdir(File directory)` Create a new directory.
    `static String` | `reader2String(Reader reader)` Read a bufferedReader into a string, using the default platform encoding.
    `static void` | `saveString2File(String fileContents, File file)` Write a string to a file, using platform encoding.
    `static void` | `saveString2File(String s, File file, String encoding)` Write a string to a file using the specified encoding.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### FileUtils

          public FileUtils()

  - ### Method Detail

    - #### 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.

    - #### 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

          public static final File createTempDir(File parent,
                                                 String prefix)

      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

          public static final File createTempFile(String prefix,
                                                  String suffix,
                                                  File tempDir)
                                           throws IOException

      Throws:
        `IOException`

    - #### copyFile

          public static final void copyFile(File file,
                                            File dir)
                                     throws IOException

      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

Copyright © 2006–2021 The Apache Software Foundation. All rights reserved.