public class ResourceGroovyMethods extends DefaultGroovyMethodsSupport
This class defines new groovy methods for Files, URLs, URIs which appear
on normal JDK classes inside the Groovy environment.
Static methods are used with the first parameter being the destination class,
i.e. public static long size(File self)
provides a size()
method for File
.
NOTE: While this class contains many 'public' static methods, it is primarily regarded as an internal class (its internal package name suggests this also). We value backwards compatibility of these methods when used within Groovy but value less backwards compatibility at the Java method call level. I.e. future versions of Groovy may remove or move a method call in this file but would normally aim to keep the method available from within Groovy.
Type Params | Return Type | Name and description |
---|---|---|
|
public static void |
append(File file, Object text) Append the text at the end of the File without writing a BOM. |
|
public static void |
append(File file, Object text, boolean writeBom) Append the text at the end of the File. |
|
public static void |
append(File file, Reader reader) Append the text supplied by the Writer at the end of the File without writing a BOM. |
|
public static void |
append(File file, Writer writer) Append the text supplied by the Writer at the end of the File without writing a BOM. |
|
public static void |
append(File file, Writer writer, boolean writeBom) Append the text supplied by the Writer at the end of the File. |
|
public static void |
append(File file, byte[] bytes) Append bytes to the end of a File. |
|
public static void |
append(File self, InputStream stream) Append binary data to the file. |
|
public static void |
append(File file, Object text, String charset) Append the text at the end of the File without writing a BOM, using a specified encoding. |
|
public static void |
append(File file, Object text, String charset, boolean writeBom) Append the text at the end of the File, using a specified encoding. |
|
public static void |
append(File file, Writer writer, String charset) Append the text supplied by the Writer at the end of the File without writing a BOM, using a specified encoding. |
|
public static void |
append(File file, Writer writer, String charset, boolean writeBom) Append the text supplied by the Writer at the end of the File, using a specified encoding. |
|
public static void |
append(File file, Reader reader, boolean writeBom) Append the text supplied by the Reader at the end of the File, using a specified encoding. |
|
public static void |
append(File file, Reader reader, String charset) Append the text supplied by the Reader at the end of the File without writing a BOM, using a specified encoding. |
|
public static void |
append(File file, Reader reader, String charset, boolean writeBom) Append the text supplied by the Reader at the end of the File, using a specified encoding. |
<T> |
public static T |
asType(File f, Class<T> c) Converts this File to a Writable or delegates to default DefaultGroovyMethods.asType. |
|
public static File |
asWritable(File file) Converts this File to a Writable. |
|
public static File |
asWritable(File file, String encoding) Allows a file to return a Writable implementation that can output itself to a Writer stream. |
|
public static File |
createParentDirectories(File self) Creates, if needed, any parent directories for this File. |
|
public static boolean |
deleteDir(File self) Deletes a directory with all contained files and subdirectories. |
|
public static long |
directorySize(File self) Calculates directory size as total size of all its files, recursively. |
|
public void |
doCall(Object[] args) |
|
public static void |
eachByte(File self, Closure closure) Traverse through each byte of this File |
|
public static void |
eachByte(File self, int bufferLen, Closure closure) Traverse through the bytes of this File, bufferLen bytes at a time. |
|
public static void |
eachByte(URL url, Closure closure) Reads the InputStream from this URL, passing each byte to the given closure. |
|
public static void |
eachByte(URL url, int bufferLen, Closure closure) Reads the InputStream from this URL, passing a byte[] and a number of bytes to the given closure. |
|
public static void |
eachDir(File self, Closure closure) Invokes the closure for each subdirectory in this directory, ignoring regular files. |
|
public static void |
eachDirMatch(File self, Object nameFilter, Closure closure) Invokes the closure for each subdirectory whose name (dir.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods.isCase method to determine if a match occurs. |
|
public static void |
eachDirRecurse(File self, Closure closure) Recursively processes each descendant subdirectory in this directory. |
|
public static void |
eachFile(File self, FileType fileType, Closure closure) Invokes the closure for each 'child' file in this 'parent' folder/directory. |
|
public static void |
eachFile(File self, Closure closure) Invokes the closure for each 'child' file in this 'parent' folder/directory. |
|
public static void |
eachFileMatch(File self, FileType fileType, Object nameFilter, Closure closure) Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods.isCase method to determine if a match occurs. |
|
public static void |
eachFileMatch(File self, Object nameFilter, Closure closure) Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods.isCase method to determine if a match occurs. |
|
public static void |
eachFileRecurse(File self, FileType fileType, Closure closure) Processes each descendant file in this directory and any sub-directories. |
|
public static void |
eachFileRecurse(File self, Closure closure) Processes each descendant file in this directory and any sub-directories. |
<T> |
public static T |
eachLine(File self, Closure<T> closure) Iterates through this file line by line. |
<T> |
public static T |
eachLine(File self, String charset, Closure<T> closure) Iterates through this file line by line. |
<T> |
public static T |
eachLine(File self, int firstLine, Closure<T> closure) Iterates through this file line by line. |
<T> |
public static T |
eachLine(File self, String charset, int firstLine, Closure<T> closure) Iterates through this file line by line. |
<T> |
public static T |
eachLine(URL url, Closure<T> closure) Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. |
<T> |
public static T |
eachLine(URL url, int firstLine, Closure<T> closure) Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. |
<T> |
public static T |
eachLine(URL url, String charset, Closure<T> closure) Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. |
<T> |
public static T |
eachLine(URL url, String charset, int firstLine, Closure<T> closure) Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. |
|
public static void |
eachObject(File self, Closure closure) Iterates through the given file object by object. |
|
public static Writable |
filterLine(File self, Closure closure) Filters the lines of a File and creates a Writable in return to stream the filtered lines. |
|
public static Writable |
filterLine(File self, String charset, Closure closure) Filters the lines of a File and creates a Writable in return to stream the filtered lines. |
|
public static void |
filterLine(File self, Writer writer, Closure closure) Filter the lines from this File, and write them to the given writer based on the given closure predicate. |
|
public static void |
filterLine(File self, Writer writer, String charset, Closure closure) Filter the lines from this File, and write them to the given writer based on the given closure predicate. |
|
public static Writable |
filterLine(URL self, Closure predicate) Filter lines from a URL using a closure predicate. |
|
public static Writable |
filterLine(URL self, String charset, Closure predicate) Filter lines from a URL using a closure predicate. |
|
public static void |
filterLine(URL self, Writer writer, Closure predicate) Uses a closure to filter lines from this URL and pass them to the given writer. |
|
public static void |
filterLine(URL self, Writer writer, String charset, Closure predicate) Uses a closure to filter lines from this URL and pass them to the given writer. |
|
public static byte[] |
getBytes(File file) Read the content of the File and returns it as a byte[]. |
|
public static byte[] |
getBytes(URL url) Read the content of this URL and returns it as a byte[]. |
|
public static byte[] |
getBytes(URL url, Map<String, ?> parameters) Read the content of this URL and returns it as a byte[]. |
|
public static String |
getText(File file, String charset) Read the content of the File using the specified encoding and return it as a String. |
|
public static String |
getText(File file) Read the content of the File and returns it as a String. |
|
public static String |
getText(URL url) Read the content of this URL and returns it as a String. |
|
public static String |
getText(URL url, Map<String, ?> parameters) Read the content of this URL and returns it as a String. |
|
public static String |
getText(URL url, String charset) Read the data from this URL and return it as a String. |
|
public static String |
getText(URL url, Map<String, ?> parameters, String charset) Read the data from this URL and return it as a String. |
|
public static File |
leftShift(File file, Object text) Write the text to the File. |
|
public static File |
leftShift(File file, byte[] bytes) Write bytes to a File. |
|
public static File |
leftShift(File file, InputStream data) Append binary data to the file. |
|
public static DataInputStream |
newDataInputStream(File file) Create a data input stream for this file |
|
public static DataOutputStream |
newDataOutputStream(File file) Creates a new data output stream for this file. |
|
public static BufferedInputStream |
newInputStream(File file) Creates a buffered input stream for this file. |
|
public static BufferedInputStream |
newInputStream(URL url) Creates a buffered input stream for this URL. |
|
public static BufferedInputStream |
newInputStream(URL url, Map<String, ?> parameters) Creates a buffered input stream for this URL. |
|
public static ObjectInputStream |
newObjectInputStream(File file) Create an object input stream for this file. |
|
public static ObjectInputStream |
newObjectInputStream(File file, ClassLoader classLoader) Create an object input stream for this file using the given class loader. |
|
public static ObjectOutputStream |
newObjectOutputStream(File file) Create an object output stream for this file. |
|
public static BufferedOutputStream |
newOutputStream(File file) Create a buffered output stream for this file. |
|
public static PrintWriter |
newPrintWriter(File file) Create a new PrintWriter for this file. |
|
public static PrintWriter |
newPrintWriter(File file, String charset) Create a new PrintWriter for this file, using specified charset. |
|
public static BufferedReader |
newReader(File file) Create a buffered reader for this file. |
|
public static BufferedReader |
newReader(File file, String charset) Create a buffered reader for this file, using the specified charset as the encoding. |
|
public static BufferedReader |
newReader(URL url) Creates a buffered reader for this URL. |
|
public static BufferedReader |
newReader(URL url, Map<String, ?> parameters) Creates a buffered reader for this URL. |
|
public static BufferedReader |
newReader(URL url, String charset) Creates a buffered reader for this URL using the given encoding. |
|
public static BufferedReader |
newReader(URL url, Map<String, ?> parameters, String charset) Creates a buffered reader for this URL using the given encoding. |
|
public static BufferedWriter |
newWriter(File file) Create a buffered writer for this file. |
|
public static BufferedWriter |
newWriter(File file, boolean append) Creates a buffered writer for this file, optionally appending to the existing file content. |
|
public static BufferedWriter |
newWriter(File file, String charset, boolean append) Helper method to create a buffered writer for a file without writing a BOM. |
|
public static BufferedWriter |
newWriter(File file, String charset, boolean append, boolean writeBom) Helper method to create a buffered writer for a file. |
|
public static BufferedWriter |
newWriter(File file, String charset) Creates a buffered writer for this file, writing data without writing a BOM, using a specified encoding. |
|
public static byte[] |
readBytes(File file) Reads the content of the file into a byte array. |
|
public static List<String> |
readLines(File file) Reads the file into a list of Strings, with one item for each line. |
|
public static List<String> |
readLines(File file, String charset) Reads the file into a list of Strings, with one item for each line. |
|
public static List<String> |
readLines(URL self) Reads the URL contents into a list, with one element for each line. |
|
public static List<String> |
readLines(URL self, String charset) Reads the URL contents into a list, with one element for each line. |
|
public static String |
relativePath(File self, File to) Relative path to file. |
|
public static boolean |
renameTo(File self, String newPathName) Renames the file. |
|
public static void |
setBytes(File file, byte[] bytes) Write the bytes from the byte array to the File. |
|
public static void |
setText(File file, String text) Synonym for write(text) allowing file.text = 'foo'. |
|
public static void |
setText(File file, String text, String charset) Synonym for write(text, charset) allowing: myFile.setText('some text', charset)or with some help from ExpandoMetaClass , you could do something like:
myFile.metaClass.setText = { String s |
|
public static long |
size(File self) Provide the standard Groovy size() method for File . |
<T> |
public static T |
splitEachLine(File self, String regex, Closure<T> closure) Iterates through this file line by line, splitting each line using the given regex separator. |
<T> |
public static T |
splitEachLine(File self, Pattern pattern, Closure<T> closure) Iterates through this file line by line, splitting each line using the given separator Pattern. |
<T> |
public static T |
splitEachLine(File self, String regex, String charset, Closure<T> closure) Iterates through this file line by line, splitting each line using the given regex separator. |
<T> |
public static T |
splitEachLine(File self, Pattern pattern, String charset, Closure<T> closure) Iterates through this file line by line, splitting each line using the given regex separator Pattern. |
<T> |
public static T |
splitEachLine(URL self, String regex, Closure<T> closure) Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator. |
<T> |
public static T |
splitEachLine(URL self, Pattern pattern, Closure<T> closure) Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator Pattern. |
<T> |
public static T |
splitEachLine(URL self, String regex, String charset, Closure<T> closure) Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator. |
<T> |
public static T |
splitEachLine(URL self, Pattern pattern, String charset, Closure<T> closure) Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator Pattern. |
|
public static URI |
toURI(CharSequence self) Transforms a CharSequence representing a URI into a URI object. |
|
public static URI |
toURI(String self) Transforms a String representing a URI into a URI object. |
|
public static URL |
toURL(CharSequence self) Transforms a CharSequence representing a URL into a URL object. |
|
public static URL |
toURL(String self) Transforms a String representing a URL into a URL object. |
|
public static void |
traverse(File self, Map<String, ?> options, Closure closure) Processes each descendant file in this directory and any sub-directories. |
|
public static void |
traverse(File self, Closure closure) Processes each descendant file in this directory and any sub-directories. |
|
public static void |
traverse(File self, Map<String, ?> options) Invokes the closure specified with key 'visit' in the options Map for each descendant file in this directory tree. |
<T> |
public static T |
withDataInputStream(File file, Closure<T> closure) Create a new DataInputStream for this file and passes it into the closure. |
<T> |
public static T |
withDataOutputStream(File file, Closure<T> closure) Create a new DataOutputStream for this file and passes it into the closure. |
|
public static Object |
withInputStream(File file, Closure closure) Create a new InputStream for this file and passes it into the closure. |
<T> |
public static T |
withInputStream(URL url, Closure<T> closure) Creates a new InputStream for this URL and passes it into the closure. |
<T> |
public static T |
withObjectInputStream(File file, Closure<T> closure) Create a new ObjectInputStream for this file and pass it to the closure. |
<T> |
public static T |
withObjectInputStream(File file, ClassLoader classLoader, Closure<T> closure) Create a new ObjectInputStream for this file associated with the given class loader and pass it to the closure. |
<T> |
public static T |
withObjectOutputStream(File file, Closure<T> closure) Create a new ObjectOutputStream for this file and then pass it to the closure. |
|
public static Object |
withOutputStream(File file, Closure closure) Creates a new OutputStream for this file and passes it into the closure. |
<T> |
public static T |
withPrintWriter(File file, Closure<T> closure) Create a new PrintWriter for this file which is then passed it into the given closure. |
<T> |
public static T |
withPrintWriter(File file, String charset, Closure<T> closure) Create a new PrintWriter with a specified charset for this file. |
<T> |
public static T |
withReader(File file, Closure<T> closure) Create a new BufferedReader for this file and then passes it into the closure, ensuring the reader is closed after the closure returns. |
<T> |
public static T |
withReader(File file, String charset, Closure<T> closure) Create a new BufferedReader for this file using the specified charset and then passes it into the closure, ensuring the reader is closed after the closure returns. |
<T> |
public static T |
withReader(URL url, Closure<T> closure) Helper method to create a new BufferedReader for a URL and then passes it to the closure. |
<T> |
public static T |
withReader(URL url, String charset, Closure<T> closure) Helper method to create a new Reader for a URL and then passes it to the closure. |
<T> |
public static T |
withWriter(File file, Closure<T> closure) Creates a new BufferedWriter for this file, passes it to the closure, and ensures the stream is flushed and closed after the closure returns. |
<T> |
public static T |
withWriter(File file, String charset, Closure<T> closure) Creates a new BufferedWriter for this file, passes it to the closure, and ensures the stream is flushed and closed after the closure returns. |
<T> |
public static T |
withWriterAppend(File file, String charset, Closure<T> closure) Create a new BufferedWriter which will append to this file. |
<T> |
public static T |
withWriterAppend(File file, Closure<T> closure) Create a new BufferedWriter for this file in append mode. |
|
public static void |
write(File file, String text) Write the text to the File without writing a BOM. |
|
public static void |
write(File file, String text, boolean writeBom) Write the text to the File. |
|
public static void |
write(File file, String text, String charset) Write the text to the File without writing a BOM, using the specified encoding. |
|
public static void |
write(File file, String text, String charset, boolean writeBom) Write the text to the File, using the specified encoding. |
Append the text at the end of the File without writing a BOM.
file
- a Filetext
- the text to append at the end of the File Append the text at the end of the File. If the default
charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) and
writeBom
is true
, the requisite byte order
mark is written to the file before the text.
file
- a Filetext
- the text to append at the end of the FilewriteBom
- whether to write a BOMAppend the text supplied by the Writer at the end of the File without writing a BOM.
file
- a Filereader
- the Reader supplying the text to append at the end of the FileAppend the text supplied by the Writer at the end of the File without writing a BOM.
file
- a Filewriter
- the Writer supplying the text to append at the end of the File Append the text supplied by the Writer at the end of the File. If the default
charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) and
writeBom
is true
, the requisite byte order
mark is written to the file before the text.
file
- a Filewriter
- the Writer supplying the text to append at the end of the FilewriteBom
- whether to write a BOMAppend bytes to the end of a File. It will not be interpreted as text.
file
- a Filebytes
- the byte array to append to the end of the FileAppend binary data to the file. It will not be interpreted as text.
self
- a Filestream
- stream to read data from.Append the text at the end of the File without writing a BOM, using a specified encoding.
file
- a Filetext
- the text to append at the end of the Filecharset
- the charset used Append the text at the end of the File, using a specified encoding. If
the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),
writeBom
is true
, and the file doesn't already
exist, the requisite byte order mark is written to the file before the
text is appended.
file
- a Filetext
- the text to append at the end of the Filecharset
- the charset usedwriteBom
- whether to write a BOMAppend the text supplied by the Writer at the end of the File without writing a BOM, using a specified encoding.
file
- a Filewriter
- the Writer supplying the text to append at the end of the Filecharset
- the charset used Append the text supplied by the Writer at the end of the File, using a specified encoding.
If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),
writeBom
is true
, and the file doesn't already
exist, the requisite byte order mark is written to the file before the
text is appended.
file
- a Filewriter
- the Writer supplying the text to append at the end of the Filecharset
- the charset usedwriteBom
- whether to write a BOM Append the text supplied by the Reader at the end of the File, using a specified encoding.
If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),
writeBom
is true
, and the file doesn't already
exist, the requisite byte order mark is written to the file before the
text is appended.
file
- a Filereader
- the Reader supplying the text to append at the end of the FilewriteBom
- whether to write a BOMAppend the text supplied by the Reader at the end of the File without writing a BOM, using a specified encoding.
file
- a Filereader
- the Reader supplying the text to append at the end of the Filecharset
- the charset used Append the text supplied by the Reader at the end of the File, using a specified encoding.
If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),
writeBom
is true
, and the file doesn't already
exist, the requisite byte order mark is written to the file before the
text is appended.
file
- a Filereader
- the Reader supplying the text to append at the end of the Filecharset
- the charset usedwriteBom
- whether to write a BOMConverts this File to a Writable or delegates to default DefaultGroovyMethods.asType.
f
- a Filec
- the desired classConverts this File to a Writable.
file
- a FileAllows a file to return a Writable implementation that can output itself to a Writer stream.
file
- a Fileencoding
- the encoding to be used when reading the file's contentsCreates, if needed, any parent directories for this File.
self
- a FileDeletes a directory with all contained files and subdirectories.
The method returns
self
- a FileCalculates directory size as total size of all its files, recursively.
self
- a file objectTraverse through each byte of this File
self
- a Fileclosure
- a closureTraverse through the bytes of this File, bufferLen bytes at a time.
self
- a FilebufferLen
- the length of the buffer to use.closure
- a 2 parameter closure which is passed the byte[] and a number of bytes successfully read.Reads the InputStream from this URL, passing each byte to the given closure. The URL stream will be closed before this method returns.
url
- url to iterate overclosure
- closure to apply to each byteReads the InputStream from this URL, passing a byte[] and a number of bytes to the given closure. The URL stream will be closed before this method returns.
url
- url to iterate overbufferLen
- the length of the buffer to use.closure
- a 2 parameter closure which is passed the byte[] and a number of bytes successfully read.Invokes the closure for each subdirectory in this directory, ignoring regular files.
self
- a File (that happens to be a folder/directory)closure
- a closure (the parameter passed is the subdirectory file)Invokes the closure for each subdirectory whose name (dir.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods.isCase method to determine if a match occurs. This method can be used with different kinds of filters like regular expressions, classes, ranges etc. Only subdirectories are matched; regular files are ignored.
self
- a File (that happens to be a folder/directory)nameFilter
- the nameFilter to perform on the name of the directory (using the DefaultGroovyMethods.isCase method)closure
- the closure to invoke Recursively processes each descendant subdirectory in this directory.
Processing consists of calling closure
passing it the current
subdirectory and then recursively processing that subdirectory.
Regular files are ignored during traversal.
self
- a File (that happens to be a folder/directory)closure
- a closureInvokes the closure for each 'child' file in this 'parent' folder/directory. Both regular files and subfolders/subdirectories can be processed depending on the fileType enum value.
self
- a File (that happens to be a folder/directory)fileType
- if normal files or directories or both should be processedclosure
- the closure to invokeInvokes the closure for each 'child' file in this 'parent' folder/directory. Both regular files and subfolders/subdirectories are processed.
self
- a File (that happens to be a folder/directory)closure
- a closure (the parameter passed is the 'child' file)Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods.isCase method to determine if a match occurs. This method can be used with different kinds of filters like regular expressions, classes, ranges etc. Both regular files and subdirectories may be candidates for matching depending on the value of fileType.
// collect names of files in baseDir matching supplied regex pattern import static groovy.io.FileType.* def names = [] baseDir.eachFileMatch FILES, ~/foo\d\.txt/, { names<<
it.name } assert names == ['foo1.txt', 'foo2.txt'] // remove all *.bak files in baseDir baseDir.eachFileMatch FILES, ~/.*\.bak/, { File bak->
bak.delete() } // print out files > 4K in size from baseDir baseDir.eachFileMatch FILES, { new File(baseDir, it).size()>
4096 }, { println "$it.name ${it.size()}" }
self
- a File (that happens to be a folder/directory)fileType
- whether normal files or directories or both should be processednameFilter
- the filter to perform on the name of the file/directory (using the DefaultGroovyMethods.isCase method)closure
- the closure to invokeInvokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods.isCase method to determine if a match occurs. This method can be used with different kinds of filters like regular expressions, classes, ranges etc. Both regular files and subdirectories are matched.
self
- a File (that happens to be a folder/directory)nameFilter
- the nameFilter to perform on the name of the file (using the DefaultGroovyMethods.isCase method)closure
- the closure to invoke Processes each descendant file in this directory and any sub-directories.
Processing consists of potentially calling closure
passing it the current
file (which may be a normal file or subdirectory) and then if a subdirectory was encountered,
recursively processing the subdirectory. Whether the closure is called is determined by whether
the file was a normal file or subdirectory and the value of fileType.
self
- a File (that happens to be a folder/directory)fileType
- if normal files or directories or both should be processedclosure
- the closure to invoke on each file Processes each descendant file in this directory and any sub-directories.
Processing consists of calling closure
passing it the current
file (which may be a normal file or subdirectory) and then if a subdirectory was encountered,
recursively processing the subdirectory.
self
- a File (that happens to be a folder/directory)closure
- a ClosureIterates through this file line by line. Each line is passed to the given 1 or 2 arg closure. The file is read using a reader which is closed before this method returns.
self
- a Fileclosure
- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)Iterates through this file line by line. Each line is passed to the given 1 or 2 arg closure. The file is read using a reader which is closed before this method returns.
self
- a Filecharset
- opens the file with a specified charsetclosure
- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)Iterates through this file line by line. Each line is passed to the given 1 or 2 arg closure. The file is read using a reader which is closed before this method returns.
self
- a FilefirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure (arg 1 is line, optional arg 2 is line number)Iterates through this file line by line. Each line is passed to the given 1 or 2 arg closure. The file is read using a reader which is closed before this method returns.
self
- a Filecharset
- opens the file with a specified charsetfirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure (arg 1 is line, optional arg 2 is line number)Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.
url
- a URL to open and readclosure
- a closure to apply on each line (arg 1 is line, optional arg 2 is line number starting at line 1)Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.
url
- a URL to open and readfirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure to apply on each line (arg 1 is line, optional arg 2 is line number)Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.
url
- a URL to open and readcharset
- opens the stream with a specified charsetclosure
- a closure to apply on each line (arg 1 is line, optional arg 2 is line number starting at line 1)Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.
url
- a URL to open and readcharset
- opens the stream with a specified charsetfirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure to apply on each line (arg 1 is line, optional arg 2 is line number)Iterates through the given file object by object.
self
- a Fileclosure
- a closureFilters the lines of a File and creates a Writable in return to stream the filtered lines.
self
is not readableself
- a Fileclosure
- a closure which returns a boolean indicating to filter
the line or notFilters the lines of a File and creates a Writable in return to stream the filtered lines.
self
- a Filecharset
- opens the file with a specified charsetclosure
- a closure which returns a boolean indicating to filter
the line or notFilter the lines from this File, and write them to the given writer based on the given closure predicate.
self
is not readableself
- a Filewriter
- a writer destination to write filtered lines toclosure
- a closure which takes each line as a parameter and returns
true
if the line should be written to this writer.Filter the lines from this File, and write them to the given writer based on the given closure predicate.
self
- a Filewriter
- a writer destination to write filtered lines tocharset
- opens the file with a specified charsetclosure
- a closure which takes each line as a parameter and returns
true
if the line should be written to this writer. Filter lines from a URL using a closure predicate. The closure
will be passed each line as a String, and it should return
true
if the line should be passed to the writer.
self
- a URLpredicate
- a closure which returns boolean and takes a line Filter lines from a URL using a closure predicate. The closure
will be passed each line as a String, and it should return
true
if the line should be passed to the writer.
self
- the URLcharset
- opens the URL with a specified charsetpredicate
- a closure which returns boolean and takes a line Uses a closure to filter lines from this URL and pass them to
the given writer. The closure will be passed each line as a String, and
it should return true
if the line should be passed to the
writer.
self
- the URLwriter
- a writer to write output topredicate
- a closure which returns true if a line should be accepted Uses a closure to filter lines from this URL and pass them to
the given writer. The closure will be passed each line as a String, and
it should return true
if the line should be passed to the
writer.
self
- the URLwriter
- a writer to write output tocharset
- opens the URL with a specified charsetpredicate
- a closure which returns true if a line should be acceptedRead the content of the File and returns it as a byte[].
file
- the file whose content we want to readRead the content of this URL and returns it as a byte[].
url
- URL to read content fromRead the content of this URL and returns it as a byte[]. The default connection parameters can be modified by adding keys to the parameters map:
url
- URL to read content fromparameters
- connection parametersRead the content of the File using the specified encoding and return it as a String.
file
- the file whose content we want to readcharset
- the charset used to read the content of the fileRead the content of the File and returns it as a String.
file
- the file whose content we want to readRead the content of this URL and returns it as a String.
url
- URL to read content fromRead the content of this URL and returns it as a String. The default connection parameters can be modified by adding keys to the parameters map:
url
- URL to read content fromparameters
- connection parametersRead the data from this URL and return it as a String. The connection stream is closed before this method returns.
url
- URL to read content fromcharset
- opens the stream with a specified charsetRead the data from this URL and return it as a String. The connection stream is closed before this method returns.
url
- URL to read content fromparameters
- connection parameterscharset
- opens the stream with a specified charsetWrite the text to the File.
file
- a Filetext
- the text to write to the FileWrite bytes to a File.
file
- a Filebytes
- the byte array to append to the end of the FileAppend binary data to the file. See append(java.io.File, java.io.InputStream)
file
- a Filedata
- an InputStream of data to write to the fileCreate a data input stream for this file
file
- a FileCreates a new data output stream for this file.
file
- a file objectCreates a buffered input stream for this file.
file
- a FileCreates a buffered input stream for this URL.
url
- a URLCreates a buffered input stream for this URL. The default connection parameters can be modified by adding keys to the parameters map:
url
- a URLparameters
- connection parametersCreate an object input stream for this file.
file
- a fileCreate an object input stream for this file using the given class loader.
file
- a fileclassLoader
- the class loader to use when loading the classCreate an object output stream for this file.
file
- a fileCreate a buffered output stream for this file.
file
- a file objectCreate a new PrintWriter for this file.
file
- a FileCreate a new PrintWriter for this file, using specified charset. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the requisite byte order mark is written to the stream before the writer is returned.
file
- a Filecharset
- the charsetCreate a buffered reader for this file.
file
- a FileCreate a buffered reader for this file, using the specified charset as the encoding.
file
- a Filecharset
- the charset for this FileCreates a buffered reader for this URL.
url
- a URLCreates a buffered reader for this URL. The default connection parameters can be modified by adding keys to the parameters map:
url
- a URLparameters
- connection parametersCreates a buffered reader for this URL using the given encoding.
url
- a URLcharset
- opens the stream with a specified charsetCreates a buffered reader for this URL using the given encoding.
url
- a URLparameters
- connection parameterscharset
- opens the stream with a specified charsetCreate a buffered writer for this file.
file
- a FileCreates a buffered writer for this file, optionally appending to the existing file content.
file
- a Fileappend
- true if data should be appended to the fileHelper method to create a buffered writer for a file without writing a BOM.
file
- a Filecharset
- the name of the encoding used to write in this fileappend
- true if in append modeHelper method to create a buffered writer for a file. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the requisite byte order mark is written to the stream before the writer is returned.
file
- a Filecharset
- the name of the encoding used to write in this fileappend
- true if in append modewriteBom
- whether to write a BOMCreates a buffered writer for this file, writing data without writing a BOM, using a specified encoding.
file
- a Filecharset
- the name of the encoding used to write in this fileReads the content of the file into a byte array.
file
- a FileReads the file into a list of Strings, with one item for each line.
file
- a FileReads the file into a list of Strings, with one item for each line.
file
- a Filecharset
- opens the file with a specified charsetReads the URL contents into a list, with one element for each line.
self
- a URLReads the URL contents into a list, with one element for each line.
self
- a URLcharset
- opens the URL with a specified charsetRelative path to file.
self
- the File
to calculate the path fromto
- the File
to calculate the path toRenames the file. It's a shortcut for File.renameTo
self
- a FilenewPathName
- The new pathname for the named filetrue
if and only if the renaming succeeded;
false
otherwiseWrite the bytes from the byte array to the File.
file
- the file to write tobytes
- the byte[] to write to the fileSynonym for write(text) allowing file.text = 'foo'.
file
- a Filetext
- the text to write to the FileSynonym for write(text, charset) allowing:
myFile.setText('some text', charset)or with some help from
ExpandoMetaClass
, you could do something like:
myFile.metaClass.setText = { String s ->
delegate.setText(s, 'UTF-8') }
myfile.text = 'some text'
file
- A Filecharset
- The charset used when writing to the filetext
- The text to write to the File Provide the standard Groovy size()
method for File
.
self
- a file objectIterates through this file line by line, splitting each line using the given regex separator. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the file are closed.
self
- a Fileregex
- the delimiting regular expressionclosure
- a closureIterates through this file line by line, splitting each line using the given separator Pattern. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression Pattern. Finally the resources used for processing the file are closed.
self
- a Filepattern
- the regular expression Pattern for the delimiterclosure
- a closureIterates through this file line by line, splitting each line using the given regex separator. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the file are closed.
self
- a Fileregex
- the delimiting regular expressioncharset
- opens the file with a specified charsetclosure
- a closureIterates through this file line by line, splitting each line using the given regex separator Pattern. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the file are closed.
self
- a Filepattern
- the regular expression Pattern for the delimitercharset
- opens the file with a specified charsetclosure
- a closureIterates through the input stream associated with this URL line by line, splitting each line using the given regex separator. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the URL are closed.
self
- a URL to open and readregex
- the delimiting regular expressionclosure
- a closureIterates through the input stream associated with this URL line by line, splitting each line using the given regex separator Pattern. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the URL are closed.
self
- a URL to open and readpattern
- the regular expression Pattern for the delimiterclosure
- a closureIterates through the input stream associated with this URL line by line, splitting each line using the given regex separator. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the URL are closed.
self
- a URL to open and readregex
- the delimiting regular expressioncharset
- opens the file with a specified charsetclosure
- a closureIterates through the input stream associated with this URL line by line, splitting each line using the given regex separator Pattern. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the URL are closed.
self
- a URL to open and readpattern
- the regular expression Pattern for the delimitercharset
- opens the file with a specified charsetclosure
- a closureTransforms a CharSequence representing a URI into a URI object.
self
- the CharSequence representing a URITransforms a String representing a URI into a URI object.
self
- the String representing a URITransforms a CharSequence representing a URL into a URL object.
self
- the CharSequence representing a URLTransforms a String representing a URL into a URL object.
self
- the String representing a URL Processes each descendant file in this directory and any sub-directories.
Processing consists of potentially calling closure
passing it the current
file (which may be a normal file or subdirectory) and then if a subdirectory was encountered,
recursively processing the subdirectory.
The traversal can be adapted by providing various options in the options
Map according
to the following keys:
def totalSize = 0 def count = 0 def sortByTypeThenName = { a, b->
a.isFile() != b.isFile() ? a.isFile()<=>
b.isFile() : a.name<=>
b.name } rootDir.traverse( type : FILES, nameFilter : ~/.*\.groovy/, preDir : { if (it.name == '.svn') return SKIP_SUBTREE }, postDir : { println "Found $count files in $it.name totalling $totalSize bytes" totalSize = 0; count = 0 }, postRoot : true sort : sortByTypeThenName ) {it->
totalSize += it.size(); count++ }
self
- a File (that happens to be a folder/directory)options
- a Map of options to alter the traversal behaviorclosure
- the Closure to invoke on each file/directory and optionally returning a FileVisitResult value
which can be used to control subsequent processingProcesses each descendant file in this directory and any sub-directories. Convenience method for traverse(java.io.File, java.util.Map, groovy.lang.Closure) when no options to alter the traversal behavior are required.
self
- a File (that happens to be a folder/directory)closure
- the Closure to invoke on each file/directory and optionally returning a FileVisitResult value
which can be used to control subsequent processingInvokes the closure specified with key 'visit' in the options Map for each descendant file in this directory tree. Convenience method for traverse(java.io.File, java.util.Map, groovy.lang.Closure) allowing the 'visit' closure to be included in the options Map rather than as a parameter.
self
- a File (that happens to be a folder/directory)options
- a Map of options to alter the traversal behaviorCreate a new DataInputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.
file
- a Fileclosure
- a closureCreate a new DataOutputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.
file
- a Fileclosure
- a closureCreate a new InputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.
file
- a Fileclosure
- a closureCreates a new InputStream for this URL and passes it into the closure. This method ensures the stream is closed after the closure returns.
url
- a URLclosure
- a closureCreate a new ObjectInputStream for this file and pass it to the closure. This method ensures the stream is closed after the closure returns.
file
- a Fileclosure
- a closureCreate a new ObjectInputStream for this file associated with the given class loader and pass it to the closure. This method ensures the stream is closed after the closure returns.
file
- a FileclassLoader
- the class loader to use when loading the classclosure
- a closureCreate a new ObjectOutputStream for this file and then pass it to the closure. This method ensures the stream is closed after the closure returns.
file
- a Fileclosure
- a closureCreates a new OutputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.
file
- a Fileclosure
- a closureCreate a new PrintWriter for this file which is then passed it into the given closure. This method ensures its the writer is closed after the closure returns.
file
- a Fileclosure
- the closure to invoke with the PrintWriterCreate a new PrintWriter with a specified charset for this file. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the requisite byte order mark is written to the stream when the writer is created. The writer is passed to the closure, and will be closed before this method returns.
file
- a Filecharset
- the charsetclosure
- the closure to invoke with the PrintWriterCreate a new BufferedReader for this file and then passes it into the closure, ensuring the reader is closed after the closure returns.
file
- a file objectclosure
- a closureCreate a new BufferedReader for this file using the specified charset and then passes it into the closure, ensuring the reader is closed after the closure returns.
file
- a file objectcharset
- the charset for this input streamclosure
- a closureHelper method to create a new BufferedReader for a URL and then passes it to the closure. The reader is closed after the closure returns.
url
- a URLclosure
- the closure to invoke with the readerHelper method to create a new Reader for a URL and then passes it to the closure. The reader is closed after the closure returns.
url
- a URLcharset
- the charset usedclosure
- the closure to invoke with the readerCreates a new BufferedWriter for this file, passes it to the closure, and ensures the stream is flushed and closed after the closure returns.
file
- a Fileclosure
- a closureCreates a new BufferedWriter for this file, passes it to the closure, and ensures the stream is flushed and closed after the closure returns. The writer will use the given charset encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the requisite byte order mark is written to the stream when the writer is created.
file
- a Filecharset
- the charset usedclosure
- a closureCreate a new BufferedWriter which will append to this file. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the requisite byte order mark is written to the stream when the writer is created. The writer is passed to the closure and will be closed before this method returns.
file
- a Filecharset
- the charset usedclosure
- a closureCreate a new BufferedWriter for this file in append mode. The writer is passed to the closure and is closed after the closure returns.
file
- a Fileclosure
- a closureWrite the text to the File without writing a BOM.
file
- a Filetext
- the text to write to the File Write the text to the File. If the default charset is
"UTF-16BE" or "UTF-16LE" (or an equivalent alias) and
writeBom
is true
, the requisite byte order
mark is written to the file before the text.
file
- a Filetext
- the text to write to the FilewriteBom
- whether to write a BOMWrite the text to the File without writing a BOM, using the specified encoding.
file
- a Filetext
- the text to write to the Filecharset
- the charset used Write the text to the File, using the specified encoding. If the given
charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) and
writeBom
is true
, the requisite byte order
mark is written to the file before the text.
file
- a Filetext
- the text to write to the Filecharset
- the charset usedwriteBom
- whether to write a BOM