public class Files extends Object
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.
Modifier and Type | Field and Description |
---|---|
protected static String |
resourcePath
Where on the classpath the gate resources are to be found
|
Constructor and Description |
---|
Files() |
Modifier and Type | Method and Description |
---|---|
static File |
fileFromURL(URL theURL)
Convert a file: URL to a
java.io.File . |
static Set<String> |
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 URL |
getGateResource(String resourceName)
Get a resource from the GATE resources directory.
|
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 URL |
getResource(String resourceName)
Get a resource from the GATE ClassLoader.
|
static byte[] |
getResourceAsByteArray(String resourceName)
Get a resource from the GATE ClassLoader as a byte array.
|
static InputStream |
getResourceAsStream(String resourceName)
Get a resource from the GATE ClassLoader as an InputStream.
|
static String |
getResourceAsString(String resourceName)
Get a resource from the GATE ClassLoader as a String.
|
static String |
getResourceAsString(String resourceName,
String encoding)
Get a resource from the GATE ClassLoader 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 File[] |
listFilesRecursively(File directory,
FileFilter filter)
Same as
File.listFiles(java.io.FileFilter)
but recursive on directories. |
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<String,String> newAttrs)
This method updates an XML element with a new set of attributes.
|
static String |
updateXmlElement(File xmlFile,
String elementName,
Map<String,String> 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.
|
protected static final String resourcePath
public static String getResourcePath()
public static String getLastPathComponent(String path)
public static String getString(String fileName) throws IOException
IOException
public static String getString(File textFile) throws IOException
IOException
public static byte[] getByteArray(File binaryFile) throws IOException
IOException
public static String getResourceAsString(String resourceName) throws IOException
resourceName
- The resource to input.IOException
public static String getResourceAsString(String resourceName, String encoding) throws IOException
encoding
- The encoding of the reader used to input the file
(may be null in which case the default encoding is used).resourceName
- The resource to input.IOException
public static String getGateResourceAsString(String resourceName) throws IOException
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.IOException
public static File writeTempFile(InputStream contentStream) throws IOException
IOException
public static File writeTempFile(String aString, String anEncoding) throws UnsupportedEncodingException, IOException
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.UnsupportedEncodingException
IOException
public static File writeTempFile(String aString) throws IOException
aString
- the String to be written. If is null then the file will be
empty.IOException
public static byte[] getResourceAsByteArray(String resourceName) throws IOException, IndexOutOfBoundsException, ArrayStoreException
public static byte[] getGateResourceAsByteArray(String resourceName) throws IOException, IndexOutOfBoundsException, ArrayStoreException
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.public static InputStream getResourceAsStream(String resourceName) throws IOException
IOException
public static InputStream getGateResourceAsStream(String resourceName) throws IOException
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.IOException
public static URL getResource(String resourceName)
URL
that can be used to retrieve the contents of the
resource.public static URL getGateResource(String resourceName)
URL
that can be used to retrieve the contents of the
resource.
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.public static Set<String> Find(String regex, String pathFile)
regex
- regular expression path that begins with pathFile
pathFile
- directory path where to search for filespathFile
that matches
regex
public static boolean rmdir(File dir)
public static String updateXmlElement(BufferedReader xml, String elementName, Map<String,String> newAttrs) throws IOException
xml
- A stream of the XML data.elementName
- The name of the element to update.newAttrs
- The new attributes to place on the element.IOException
public static String updateXmlElement(File xmlFile, String elementName, Map<String,String> newAttrs) throws IOException
xmlFile
- An XML file.elementName
- The name of the element to update.newAttrs
- The new attributes to place on the element.IOException
public static File fileFromURL(URL theURL) throws IllegalArgumentException
java.io.File
. First tries to parse
the URL's toExternalForm as a URI and create the File object from that
URI. If this fails, just uses the path part of the URL. This handles
URLs that contain spaces or other unusual characters, both as literals and
when encoded as (e.g.) %20.IllegalArgumentException
- if the URL is not convertable into a
File.public static File[] listFilesRecursively(File directory, FileFilter filter)
File.listFiles(java.io.FileFilter)
but recursive on directories.directory
- file path to start the search, will not be include
in the resultsfilter
- filter apply to the searchdirectory
. The array will be empty if the directory is
empty. Returns null if this abstract pathname does not denote a
directory, or if an I/O error occurs.Copyright © 2024 GATE. All rights reserved.