public class NioExtensions extends DefaultGroovyMethodsSupport
This class defines new groovy methods for Readers, Writers, InputStreams and
OutputStreams 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 T eachLine(InputStream self, Closure c)
provides a eachLine(Closure c)
method for InputStream
.
Type Params | Return Type | Name and description |
---|---|---|
|
public static void |
append(Path self, Object text) Append the text at the end of the Path without writing a BOM. |
|
public static void |
append(Path file, Reader reader) Append the text supplied by the Writer at the end of the File without writing a BOM. |
|
public static void |
append(Path file, Writer writer) Append the text supplied by the Writer at the end of the File without writing a BOM. |
|
public static void |
append(Path self, byte[] bytes) Append bytes to the end of a Path. |
|
public static void |
append(Path self, InputStream stream) Append binary data to the file. |
|
public static void |
append(Path self, Object text, boolean writeBom) Append the text at the end of the Path. |
|
public static void |
append(Path self, Object text, String charset) Append the text at the end of the Path without writing a BOM, using a specified encoding. |
|
public static void |
append(Path self, Object text, String charset, boolean writeBom) Append the text at the end of the Path, using a specified encoding. |
|
public static void |
append(Path file, Writer writer, boolean writeBom) Append the text supplied by the Writer at the end of the File, using a specified encoding. |
|
public static void |
append(Path 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(Path 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(Path 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(Path 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(Path file, Reader reader, String charset, boolean writeBom) Append the text supplied by the Reader at the end of the File, using a specified encoding. |
|
public static boolean |
asBoolean(Path path) Coerce the path to a boolean value. |
<T> |
public static T |
asType(Path path, Class<T> c) Converts this Path to a Writable or delegates to default DefaultGroovyMethods.asType. |
|
public static Path |
asWritable(Path self) Converts this Path to a Writable. |
|
public static Path |
asWritable(Path self, String encoding) Allows a file to return a Writable implementation that can output itself to a Writer stream. |
|
public static Path |
createParentDirectories(Path self, FileAttribute<?> attrs) Creates, if needed, any parent directories for the file associated with this Path. |
|
public static boolean |
deleteDir(Path self) Deletes a directory with all contained files and subdirectories. |
|
public static void |
eachByte(Path self, Closure closure) Traverse through each byte of this Path |
|
public static void |
eachByte(Path self, int bufferLen, Closure closure) Traverse through the bytes of this Path, bufferLen bytes at a time. |
|
public static void |
eachDir(Path self, Closure closure) Invokes the closure for each subdirectory in this directory, ignoring regular files. |
|
public static void |
eachDirMatch(Path 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(java.lang.Object, java.lang.Object) method to determine if a match occurs. |
|
public static void |
eachDirRecurse(Path self, Closure closure) Recursively processes each descendant subdirectory in this directory. |
|
public static void |
eachFile(Path self, FileType fileType, Closure closure) Invokes the closure for each 'child' file in this 'parent' folder/directory. |
|
public static void |
eachFile(Path self, Closure closure) Invokes the closure for each 'child' file in this 'parent' folder/directory. |
|
public static void |
eachFileMatch(Path 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(Path 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(Path self, FileType fileType, Closure closure) Processes each descendant file in this directory and any subdirectories. |
|
public static void |
eachFileRecurse(Path self, Closure closure) Processes each descendant file in this directory and any subdirectories. |
<T> |
public static T |
eachLine(Path self, Closure<T> closure) Iterates through this path line by line. |
<T> |
public static T |
eachLine(Path self, String charset, Closure<T> closure) Iterates through this file line by line. |
<T> |
public static T |
eachLine(Path self, int firstLine, Closure<T> closure) Iterates through this file line by line. |
<T> |
public static T |
eachLine(Path self, String charset, int firstLine, Closure<T> closure) Iterates through this file line by line. |
|
public static void |
eachObject(Path self, Closure closure) Iterates through the given file object by object. |
|
public static boolean |
exists(Path path, LinkOption options) Tests whether the file at the path exists. |
|
public static Writable |
filterLine(Path self, Closure closure) Filters the lines of a Path and creates a Writable in return to stream the filtered lines. |
|
public static Writable |
filterLine(Path self, String charset, Closure closure) Filters the lines of a Path and creates a Writable in return to stream the filtered lines. |
|
public static void |
filterLine(Path self, Writer writer, Closure closure) Filter the lines from this Path, and write them to the given writer based on the given closure predicate. |
|
public static void |
filterLine(Path self, Writer writer, String charset, Closure closure) Filter the lines from this Path, and write them to the given writer based on the given closure predicate. |
|
public static String |
getBaseName(Path self) Provide the path's name without an extension. |
|
public static byte[] |
getBytes(Path self) Read the content of the Path and returns it as a byte[]. |
|
public static String |
getExtension(Path self) Provide the extension of this path. |
|
public static String |
getName(Path self) Provide the name of the file or directory. |
|
public static String |
getText(Path self, String charset) Read the content of the Path using the specified encoding and return it as a String. |
|
public static String |
getText(Path self) Read the content of the Path and returns it as a String. |
|
public static Path |
leftShift(Path self, Object text) Write the text to the Path. |
|
public static Path |
leftShift(Path self, byte[] bytes) Write bytes to a Path. |
|
public static Path |
leftShift(Path path, InputStream data) Append binary data to the file. |
|
public static DataInputStream |
newDataInputStream(Path self) Create a data input stream for this file |
|
public static DataOutputStream |
newDataOutputStream(Path self) Creates a new data output stream for this file. |
|
public static BufferedInputStream |
newInputStream(Path self) Creates a buffered input stream for this file. |
|
public static ObjectInputStream |
newObjectInputStream(Path self) Create an object input stream for this file. |
|
public static ObjectInputStream |
newObjectInputStream(Path self, ClassLoader classLoader) Create an object input stream for this path using the given class loader. |
|
public static ObjectOutputStream |
newObjectOutputStream(Path self) Create an object output stream for this path. |
|
public static BufferedOutputStream |
newOutputStream(Path self) Create a buffered output stream for this file. |
|
public static PrintWriter |
newPrintWriter(Path self) Create a new PrintWriter for this file. |
|
public static PrintWriter |
newPrintWriter(Path self, String charset) Create a new PrintWriter for this file, using specified charset. |
|
public static BufferedReader |
newReader(Path self) Create a buffered reader for this file. |
|
public static BufferedReader |
newReader(Path self, String charset) Create a buffered reader for this file, using the specified charset as the encoding. |
|
public static BufferedWriter |
newWriter(Path self) Create a buffered writer for this file. |
|
public static BufferedWriter |
newWriter(Path self, boolean append) Creates a buffered writer for this file, optionally appending to the existing file content. |
|
public static BufferedWriter |
newWriter(Path self, String charset, boolean append) Helper method to create a buffered writer for a file without writing a BOM. |
|
public static BufferedWriter |
newWriter(Path self, String charset, boolean append, boolean writeBom) Helper method to create a buffered writer for a file. |
|
public static BufferedWriter |
newWriter(Path self, String charset) Creates a buffered writer for this file without writing a BOM, writing data using the given encoding. |
|
public static byte[] |
readBytes(Path self) Reads the content of the file into a byte array. |
|
public static List<String> |
readLines(Path self) Reads the file into a list of Strings, with one item for each line. |
|
public static List<String> |
readLines(Path self, String charset) Reads the file into a list of Strings, with one item for each line. |
|
public static boolean |
renameTo(Path self, String newPathName) Renames a file. |
|
public static boolean |
renameTo(Path self, URI newPathName) Renames a file. |
|
public static void |
setBytes(Path self, byte[] bytes) Write the bytes from the byte array to the Path. |
|
public static void |
setText(Path self, String text) Synonym for write(text) allowing file.text = 'foo'. |
|
public static void |
setText(Path self, 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(Path self) Provide the standard Groovy size() method for Path . |
<T> |
public static T |
splitEachLine(Path 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(Path 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(Path 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(Path self, Pattern pattern, String charset, Closure<T> closure) Iterates through this file line by line, splitting each line using the given regex separator Pattern. |
|
public static void |
traverse(Path self, Map<String, Object> options, Closure closure) Processes each descendant file in this directory and any subdirectories. |
|
public static void |
traverse(Path self, Closure closure) Processes each descendant file in this directory and any subdirectories. |
|
public static void |
traverse(Path self, Map<String, Object> options) Invokes the closure specified with key 'visit' in the options Map for each descendant file in this directory tree. |
<T> |
public static T |
withCloseable(Closeable self, Closure<T> action) #deprecated use the variant in IOGroovyMethods |
<T> |
public static T |
withDataInputStream(Path self, Closure<T> closure) Create a new DataInputStream for this file and passes it into the closure. |
<T> |
public static T |
withDataOutputStream(Path self, Closure<T> closure) Create a new DataOutputStream for this file and passes it into the closure. |
<T> |
public static T |
withInputStream(Path self, Closure<T> closure) Create a new InputStream for this file and passes it into the closure. |
<T> |
public static T |
withObjectInputStream(Path path, Closure<T> closure) Create a new ObjectInputStream for this file and pass it to the closure. |
<T> |
public static T |
withObjectInputStream(Path self, 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(Path self, Closure<T> closure) Create a new ObjectOutputStream for this path and then pass it to the closure. |
<T> |
public static T |
withOutputStream(Path self, Closure<T> closure) Creates a new OutputStream for this file and passes it into the closure. |
<T> |
public static T |
withPrintWriter(Path self, Closure<T> closure) Create a new PrintWriter for this file which is then passed it into the given closure. |
<T> |
public static T |
withPrintWriter(Path self, String charset, Closure<T> closure) Create a new PrintWriter with a specified charset for this file. |
<T> |
public static T |
withReader(Path self, 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(Path self, 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 |
withWriter(Path self, 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(Path self, 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 |
withWriter(Path self, String charset, boolean writeBom, 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(Path self, String charset, Closure<T> closure) Create a new BufferedWriter which will append to this file. |
<T> |
public static T |
withWriterAppend(Path self, String charset, boolean writeBom, Closure<T> closure) Create a new BufferedWriter which will append to this file. |
<T> |
public static T |
withWriterAppend(Path self, Closure<T> closure) Create a new BufferedWriter for this file in append mode. |
|
public static void |
write(Path self, String text) Write the text to the Path without writing a BOM . |
|
public static void |
write(Path self, String text, boolean writeBom) Write the text to the Path. |
|
public static void |
write(Path self, String text, String charset) Write the text to the Path without writing a BOM, using the specified encoding. |
|
public static void |
write(Path self, String text, String charset, boolean writeBom) Write the text to the Path, using the specified encoding. |
Append the text at the end of the Path without writing a BOM.
self
- a Pathtext
- the text to append at the end of the PathAppend the text supplied by the Writer at the end of the File without writing a BOM.
file
- a Pathreader
- 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 FileAppend bytes to the end of a Path. It will not be interpreted as text.
self
- a Pathbytes
- the byte array to append to the end of the PathAppend binary data to the file. It will not be interpreted as text.
self
- a Pathstream
- stream to read data from. Append the text at the end of the Path. 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.
self
- a Pathtext
- the text to append at the end of the PathwriteBom
- whether to write the BOMAppend the text at the end of the Path without writing a BOM, using a specified encoding.
self
- a Pathtext
- the text to append at the end of the Pathcharset
- the charset used Append the text at the end of the Path, 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.
self
- a Pathtext
- the text to append at the end of the Pathcharset
- the charset usedwriteBom
- whether to write the BOM 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 FilewriteBom
- whether to write the 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 the 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 the 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 the BOM Coerce the path to a boolean
value.
path
- a Path
objecttrue
if the file at the path exists, false
otherwiseConverts this Path to a Writable or delegates to default DefaultGroovyMethods.asType.
path
- a Pathc
- the desired classConverts this Path to a Writable.
self
- a PathAllows a file to return a Writable implementation that can output itself to a Writer stream.
self
- a Pathencoding
- the encoding to be used when reading the file's contentsCreates, if needed, any parent directories for the file associated with this Path.
self
- a PathDeletes a directory with all contained files and subdirectories.
The method returns
self
- a PathTraverse through each byte of this Path
self
- a Pathclosure
- a closureTraverse through the bytes of this Path, bufferLen bytes at a time.
self
- a PathbufferLen
- 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 Path (that happens to be a folder/directory)closure
- a closure (the parameter is the Path for 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(java.lang.Object, java.lang.Object) 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 Path (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 Path (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 Path (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 Path (that happens to be a folder/directory)closure
- a closure (the parameter is the Path for 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/, { Path bak->
bak.delete() } // print out files > 4K in size from baseDir baseDir.eachFileMatch FILES, { new Path(baseDir, it).size()>
4096 }, { println "$it.name ${it.size()}" }
self
- a Path (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 Path (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 subdirectories.
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 Path (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 subdirectories.
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 Path (that happens to be a folder/directory)closure
- a ClosureIterates through this path 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 Pathclosure
- 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 Pathcharset
- 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 PathfirstLine
- 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 Pathcharset
- 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 given file object by object.
self
- a Path
objectclosure
- a closureTests whether the file at the path exists.
path
- a Path
objectoptions
- options indicating how symbolic links are handled .true
if the file exists; false
if the file does not exist or its existence cannot be determinedFilters the lines of a Path and creates a Writable in return to stream the filtered lines.
self
is not readableself
- a Pathclosure
- a closure which returns a boolean indicating to filter
the line or notFilters the lines of a Path and creates a Writable in return to stream the filtered lines.
self
- a Pathcharset
- opens the file with a specified charsetclosure
- a closure which returns a boolean indicating to filter
the line or notFilter the lines from this Path, and write them to the given writer based on the given closure predicate.
self
is not readableself
- a Pathwriter
- 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 Path, and write them to the given writer based on the given closure predicate.
self
- a Pathwriter
- 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.Provide the path's name without an extension.
self
- a file objectRead the content of the Path and returns it as a byte[].
self
- the file whose content we want to readProvide the extension of this path.
self
- a path objectProvide the name of the file or directory.
self
- a path objectRead the content of the Path using the specified encoding and return it as a String.
self
- the file whose content we want to readcharset
- the charset used to read the content of the fileRead the content of the Path and returns it as a String.
self
- the file whose content we want to readWrite the text to the Path.
self
- a Pathtext
- the text to write to the PathWrite bytes to a Path.
self
- a Pathbytes
- the byte array to append to the end of the PathAppend binary data to the file. See append(Path, java.io.InputStream)
path
- a Pathdata
- an InputStream of data to write to the fileCreate a data input stream for this file
self
- a PathCreates a new data output stream for this file.
self
- a file objectCreates a buffered input stream for this file.
self
- a PathCreate an object input stream for this file.
self
- a Path
objectCreate an object input stream for this path using the given class loader.
self
- a Path
objectclassLoader
- the class loader to use when loading the classCreate an object output stream for this path.
self
- a Path
objectCreate a buffered output stream for this file.
self
- a file objectCreate a new PrintWriter for this file.
self
- a PathCreate a new PrintWriter for this file, using specified charset.
self
- a Pathcharset
- the charsetCreate a buffered reader for this file.
self
- a PathCreate a buffered reader for this file, using the specified charset as the encoding.
self
- a Pathcharset
- the charset for this PathCreate a buffered writer for this file.
self
- a PathCreates a buffered writer for this file, optionally appending to the existing file content.
self
- a Pathappend
- true if data should be appended to the fileHelper method to create a buffered writer for a file without writing a BOM.
self
- a Pathcharset
- 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.
self
- a Pathcharset
- 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 without writing a BOM, writing data using the given encoding.
self
- a Pathcharset
- the name of the encoding used to write in this fileReads the content of the file into a byte array.
self
- a PathReads the file into a list of Strings, with one item for each line.
self
- a PathReads the file into a list of Strings, with one item for each line.
self
- a Pathcharset
- opens the file with a specified charsetRenames a file.
self
- a PathnewPathName
- The new pathname for the named filetrue
if and only if the renaming succeeded;
false
otherwiseRenames a file.
self
- a PathnewPathName
- The new target path specified as a URI objecttrue
if and only if the renaming succeeded;
false
otherwiseWrite the bytes from the byte array to the Path.
self
- the file to write tobytes
- the byte[] to write to the fileSynonym for write(text) allowing file.text = 'foo'.
self
- a Pathtext
- the text to write to the PathSynonym 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'
self
- A Pathcharset
- The charset used when writing to the filetext
- The text to write to the Path Provide the standard Groovy size()
method for Path
.
self
- a Path
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 Pathregex
- 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 Pathpattern
- 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 Pathregex
- 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 Pathpattern
- the regular expression Pattern for the delimitercharset
- opens the file with a specified charsetclosure
- a closure Processes each descendant file in this directory and any subdirectories.
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 Path (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 subdirectories. Convenience method for traverse(Path, java.util.Map, groovy.lang.Closure) when no options to alter the traversal behavior are required.
self
- a Path (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(Path, java.util.Map, groovy.lang.Closure) allowing the 'visit' closure to be included in the options Map rather than as a parameter.
self
- a Path (that happens to be a folder/directory)options
- a Map of options to alter the traversal behavior#deprecated use the variant in IOGroovyMethods
Create a new DataInputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.
self
- a Pathclosure
- 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.
self
- a Pathclosure
- 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.
self
- a Pathclosure
- 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.
path
- a Pathclosure
- 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.
self
- a PathclassLoader
- the class loader to use when loading the classclosure
- a closureCreate a new ObjectOutputStream for this path and then pass it to the closure. This method ensures the stream is closed after the closure returns.
self
- a Pathclosure
- 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.
self
- a Pathclosure
- a closureCreate a new PrintWriter for this file which is then passed it into the given closure. This method ensures the writer is closed after the closure returns.
self
- a Pathclosure
- the closure to invoke with the PrintWriterCreate a new PrintWriter with a specified charset for this file. The writer is passed to the closure, and will be closed before this method returns.
self
- a Pathcharset
- 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.
self
- 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. The writer will use the given charset encoding, but will not write a BOM.
self
- a file objectcharset
- the charset for this input streamclosure
- 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 not write a BOM.
self
- a Pathclosure
- 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, but will not write a BOM.
self
- a Pathcharset
- the charset usedclosure
- a 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.
The writer will use the given charset 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 stream when the writer is created.
self
- a Pathcharset
- the charset usedwriteBom
- whether to write the BOMclosure
- a closureCreate a new BufferedWriter which will append to this file. The writer is passed to the closure and will be closed before this method returns. The writer will use the given charset encoding, but will not write a BOM.
self
- a Pathcharset
- the charset usedclosure
- a closure Create a new BufferedWriter which will append to this
file. The writer is passed to the closure and will be closed before
this method returns. The writer will use the given charset 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 stream when the writer is created.
self
- a Pathcharset
- the charset usedwriteBom
- whether to write the BOMclosure
- 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. The writer will not write a BOM.
self
- a Pathclosure
- a closureWrite the text to the Path without writing a BOM .
self
- a Pathtext
- the text to write to the Path Write the text to the Path. 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.
self
- a Pathtext
- the text to write to the PathwriteBom
- whether to write the BOMWrite the text to the Path without writing a BOM, using the specified encoding.
self
- a Pathtext
- the text to write to the Pathcharset
- the charset used Write the text to the Path, 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.
self
- a Pathtext
- the text to write to the Pathcharset
- the charset usedwriteBom
- whether to write a BOM