## gate.util
Class Files

    java.lang.Object
      |
      +--gate.util.Files

public class Files
extends Object

Some utilities for use with Files and with resources.

Note that there is a terminology conflict between the use of "resources" here and `gate.Resource` and its inheritors.

Java "resources" are files that live on the CLASSPATH or in a Jar file that are not `.class` files. For example: a `.gif` file that is used by a GUI, or one of the XML files used for testing GATE's document format facilities. This class allows you to access these files in various ways (as streams, as byte arrays, etc.).

GATE resources are components (Java Beans) that provide all of the natural language processing capabilities of a GATE-based system, and the language data that such systems analsyse and produce. For example: parsers, lexicons, generators, corpora.

Where we say "resource" in this class we mean Java resource; elsewhere in the system we almost always mean GATE resource.

Field Summary
`private static boolean` `DEBUG` Debug flag
`(package private) static long` `resourceIndex` Used to generate temporary resources names
`protected static String` `resourcePath` Where on the classpath the gate resources are to be found

Constructor Summary
`Files()`

Method Summary
`static Set` `Find(String regex, String pathFile)` This method takes a regular expression and a directory name and returns the set of Files that match the pattern under that directory.
`static byte[]` `getByteArray(File binaryFile)` Get a byte array representing the contents of a binary file.
`static byte[]` `getGateResourceAsByteArray(String resourceName)` Get a resource from the GATE resources directory as a byte array.
`static InputStream` `getGateResourceAsStream(String resourceName)` Get a resource from the GATE resources directory as an InputStream.
`static String` `getGateResourceAsString(String resourceName)` Get a resource from the GATE resources directory as a String.
`static String` `getLastPathComponent(String path)` It returns the last component in a file path.
`static byte[]` `getResourceAsByteArray(String resourceName)` Get a resource from the classpath as a byte array.
`static InputStream` `getResourceAsStream(String resourceName)` Get a resource from the classpath as an InputStream.
`static String` `getResourceAsString(String resourceName)` Get a resource from the classpath as a String.
`static String` `getResourcePath()` Gets the path for the gate resources within the classpath
`static String` `getString(File textFile)` Get a string representing the contents of a text file.
`static String` `getString(String fileName)` Get a string representing the contents of a text file.
`static boolean` `rmdir(File dir)` Recursively remove a directory even if it contains other files or directories.
`static String` `updateXmlElement(BufferedReader xml, String elementName, Map newAttrs)` This method updates an XML element with a new set of attributes.
`static String` `updateXmlElement(File xmlFile, String elementName, Map newAttrs)` This method updates an XML element in an XML file with a new set of attributes.
`static File` `writeTempFile(InputStream contentStream)` Writes a temporary file into the default temporary directory, form an InputStream a unique ID is generated and associated automaticaly with the file name...
`static File` `writeTempFile(String aString)` Writes aString into a temporary file located inside the default temporary directory defined by JVM, using the default encoding.
`static File` `writeTempFile(String aString, String anEncoding)` Writes aString into a temporary file located inside the default temporary directory defined by JVM, using the specific anEncoding.

Methods inherited from class java.lang.Object
`<clinit>, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait`

Field Detail

### DEBUG

    private static final boolean DEBUG

  Debug flag

### resourceIndex

    static long resourceIndex

  Used to generate temporary resources names

### resourcePath

    protected static String resourcePath

  Where on the classpath the gate resources are to be found

Constructor Detail

### Files

    public Files()

Method Detail

### getResourcePath

    public static String getResourcePath()

  Gets the path for the gate resources within the classpath

### getLastPathComponent

    public static String getLastPathComponent(String path)

  It returns the last component in a file path. It takes E.g: d:/tmp/file.txt and returns file.txt

### getString

    public static String getString(String fileName)
                            throws IOException

  Get a string representing the contents of a text file.

### getString

    public static String getString(File textFile)
                            throws IOException

  Get a string representing the contents of a text file.

### getByteArray

    public static byte[] getByteArray(File binaryFile)
                               throws IOException

  Get a byte array representing the contents of a binary file.

### getResourceAsString

    public static String getResourceAsString(String resourceName)
                                      throws IOException

  Get a resource from the classpath as a String.

### getGateResourceAsString

    public static String getGateResourceAsString(String resourceName)
                                          throws IOException

  Get a resource from the GATE resources directory as a String. The resource name should be relative to `resourcePath` which is equal with `gate/resources`; e.g. for a resource stored as `gate/resources/jape/Test11.jape`, this method should be passed the name `jape/Test11.jape`.

### writeTempFile

    public static File writeTempFile(InputStream contentStream)
                              throws IOException

  Writes a temporary file into the default temporary directory, form an InputStream a unique ID is generated and associated automaticaly with the file name...

### writeTempFile

    public static File writeTempFile(String aString,
                                     String anEncoding)
                              throws UnsupportedEncodingException,
                                     IOException

  Writes aString into a temporary file located inside the default temporary directory defined by JVM, using the specific anEncoding. An unique ID is generated and associated automaticaly with the file name.
  Parameters:
    `aString` - the String to be written. If is null then the file will be empty.
    `anEncoding` - the encoding to be used. If is null then the default encoding will be used.
  Returns:
    the tmp file containing the string.

### writeTempFile

    public static File writeTempFile(String aString)
                              throws IOException

  Writes aString into a temporary file located inside the default temporary directory defined by JVM, using the default encoding. An unique ID is generated and associated automaticaly with the file name.
  Parameters:
    `aString` - the String to be written. If is null then the file will be empty.
  Returns:
    the tmp file containing the string.

### getResourceAsByteArray

    public static byte[] getResourceAsByteArray(String resourceName)
                                         throws IOException,
                                                IndexOutOfBoundsException,
                                                ArrayStoreException

  Get a resource from the classpath as a byte array.

### getGateResourceAsByteArray

    public static byte[] getGateResourceAsByteArray(String resourceName)
                                             throws IOException,
                                                    IndexOutOfBoundsException,
                                                    ArrayStoreException

  Get a resource from the GATE resources directory as a byte array. The resource name should be relative to `resourcePath`which is equal with `gate/resources`; e.g. for a resource stored as `gate/resources/jape/Test11.jape`, this method should be passed the name `jape/Test11.jape`.``

### getResourceAsStream

    public static InputStream getResourceAsStream(String resourceName)
                                           throws IOException

  Get a resource from the classpath as an InputStream.

### getGateResourceAsStream

    public static InputStream getGateResourceAsStream(String resourceName)
                                               throws IOException

  Get a resource from the GATE resources directory as an InputStream. The resource name should be relative to `resourcePath`which is equal with `gate/resources`; e.g. for a resource stored as `gate/resources/jape/Test11.jape`, this method should be passed the name `jape/Test11.jape`.``

### Find

    public static Set Find(String regex,
                           String pathFile)

  This method takes a regular expression and a directory name and returns the set of Files that match the pattern under that directory.

### rmdir

    public static boolean rmdir(File dir)

  Recursively remove a directory even if it contains other files or directories. Returns true when the directory and all its contents are successfully removed, else false.

### updateXmlElement

    public static String updateXmlElement(BufferedReader xml,
                                          String elementName,
                                          Map newAttrs)
                                   throws IOException

  This method updates an XML element with a new set of attributes. If the element is not found the XML is unchanged. The attributes keys and values must all be Strings.
  Parameters:
    `xml` - A stream of the XML data.
    `elementName` - The name of the element to update.
    `newAttrs` - The new attributes to place on the element.
  Returns:
    A string of the whole XML source, with the element updated.

### updateXmlElement

    public static String updateXmlElement(File xmlFile,
                                          String elementName,
                                          Map newAttrs)
                                   throws IOException

  This method updates an XML element in an XML file with a new set of attributes. If the element is not found the XML file is unchanged. The attributes keys and values must all be Strings.
  Parameters:
    `xmlFile` - An XML file.
    `elementName` - The name of the element to update.
    `newAttrs` - The new attributes to place on the element.
  Returns:
    A string of the whole XML file, with the element updated (the file is also overwritten).