net.sf.okapi.common

## Class FileUtil

- java.lang.Object
- - net.sf.okapi.common.FileUtil

-     public final class FileUtil
      extends java.lang.Object

  Helper methods for manipulating files.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `FileUtil()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static java.io.File` | `createTempFile(java.lang.String prefix)`
    `static java.io.File` | `createTempFile(java.lang.String prefix, java.lang.String extension)`
    `static void` | `deleteAllFilesInDirectory(java.lang.String directoryPath)` Deletes all files in the specified directory.
    `static java.net.URL` | `fileToUrl(java.io.File file)`
    `static java.util.Collection<java.io.File>` | `getFilteredFiles(java.io.File directory, java.io.FilenameFilter filter, boolean recurse)` Gets a collection of the files in a given directory.
    `static java.io.File[]` | `getFilteredFilesAsArray(java.io.File directory, java.io.FilenameFilter filter, boolean recurse)` Gets an array of the files in a given directory.
    `static java.lang.String` | `getLocaleBasedFile(java.lang.String baseName, java.lang.String extension, LocaleId locale)` Return a path to a locale based resource using the standard java property resource resolution.
    `static java.lang.String` | `getPartBeforeFile(java.net.URI uri)` Gets the URI part before the file name.
    `static java.util.List<java.lang.String>` | `guessLanguages(java.io.Reader reader)` Tries to guess the language(s) declared in the given input.
    `static java.util.List<java.lang.String>` | `guessLanguages(java.lang.String path)` Tries to guess the language(s) declared in the given input file.
    `static boolean` | `isXliffSegmented(java.io.Reader reader)` Scans xliff file to see if it is segmented (has <seg-source>)
    `static boolean` | `isXliffSegmented(java.lang.String path)` Scans xliff file to see if it is segmented (has <seg-source>)
    `static java.io.File` | `urlToFile(java.net.URL url)`

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

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

- - ### Constructor Detail

    - #### FileUtil

          public FileUtil()

  - ### Method Detail

    - #### getFilteredFilesAsArray

          public static java.io.File[] getFilteredFilesAsArray(java.io.File directory,
                                                               java.io.FilenameFilter filter,
                                                               boolean recurse)

      Gets an array of the files in a given directory.

      This method searches all `File`s recursively that pass the `FilenameFilter`. Adapted from http://snippets.dzone.com/posts/show/1875

      Parameters:
        `directory` - root directory
        `filter` - `FilenameFilter` used to filter the File candidates
        `recurse` - true to recurse in the sub-directories, false to not.
      Returns:
        an array of `File`s (File[])

    - #### getFilteredFiles

          public static java.util.Collection<java.io.File> getFilteredFiles(java.io.File directory,
                                                                            java.io.FilenameFilter filter,
                                                                            boolean recurse)

      Gets a collection of the files in a given directory.

      This method search all `File`s recursively that pass the `FilenameFilter`. Adapted from http://snippets.dzone.com/posts/show/1875

      Parameters:
        `directory` - root directory
        `filter` - `FilenameFilter` used to filter the File candidates
        `recurse` - true to recurse in the sub-directories, false to not.
      Returns:
        `Collection` of `File`s

    - #### guessLanguages

          public static java.util.List<java.lang.String> guessLanguages(java.lang.String path)

      Tries to guess the language(s) declared in the given input file. The method should work with XLIFF, TMX, TTX and TS files.

      The methods looks in the file line by line, in the 10 first KB, or until a source and at least one target are detected, whichever comes first.

      The encoding for the file is determined based on the BOM, if present.

      Parameters:
        `path` - the full path of the file to process.
      Returns:
        a list of strings that can be empty (never null). The first string is the possible source language, the next strings are the potential target languages.

    - #### guessLanguages

          public static java.util.List<java.lang.String> guessLanguages(java.io.Reader reader)

      Tries to guess the language(s) declared in the given input. The method should work with XLIFF, TMX, TTX and TS files.

      The methods looks in the file line by line, in the 10 first KB, or until a source and at least one target are detected, whichever comes first.

      Parameters:
        `reader` - a reader providing the content to examine. This reader will be closed by this method.
      Returns:
        a list of strings that can be empty (never null). The first string is the possible source language, the next strings are the potential target languages.

    - #### isXliffSegmented

          public static boolean isXliffSegmented(java.lang.String path)

      Scans xliff file to see if it is segmented (has <seg-source>)
      Parameters:
        `path` - the full path of the xliff file to process.
      Returns:
        a true if xliff is segmented, false otherwise.

    - #### isXliffSegmented

          public static boolean isXliffSegmented(java.io.Reader reader)

      Scans xliff file to see if it is segmented (has <seg-source>)
      Parameters:
        `reader` - a reader providing the xliff content to examine. This reader will be closed by this method.
      Returns:
        a true if xliff is segmented, false otherwise.

    - #### deleteAllFilesInDirectory

          public static void deleteAllFilesInDirectory(java.lang.String directoryPath)

      Deletes all files in the specified directory.
      Parameters:
        `directoryPath` - - the path to the directory
      Throws:
        `OkapiIOException` - if a file cannot be deleted.

    - #### getLocaleBasedFile

          public static java.lang.String getLocaleBasedFile(java.lang.String baseName,
                                                            java.lang.String extension,
                                                            LocaleId locale)

      Return a path to a locale based resource using the standard java property resource resolution. Works with any kind of files e.g., segmenter_en_US.srx or content_fr_FR.html

      WARNING: Assumes default classLoader only!!

      Parameters:
        `baseName` - base name of the resource
        `extension` - resource file extension
        `locale` - locale of the resource we are looking for
      Returns:
        the path to the resource or null if not found

    - #### getPartBeforeFile

          public static java.lang.String getPartBeforeFile(java.net.URI uri)

      Gets the URI part before the file name.
      Parameters:
        `uri` - The URI to process.
      Returns:
        the URI part before the file name.

    - #### createTempFile

          public static java.io.File createTempFile(java.lang.String prefix,
                                                    java.lang.String extension)

    - #### createTempFile

          public static java.io.File createTempFile(java.lang.String prefix)

    - #### fileToUrl

          public static java.net.URL fileToUrl(java.io.File file)

    - #### urlToFile

          public static java.io.File urlToFile(java.net.URL url)