public class NioGroovyMethods extends DefaultGroovyMethodsSupport
public static T eachLine(InputStream self, Closure c)
provides a eachLine(Closure c)
method for InputStream
.
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.Constructor and Description |
---|
NioGroovyMethods() |
Modifier and Type | Method and Description |
---|---|
static void |
append(java.nio.file.Path self,
byte[] bytes)
Append bytes to the end of a Path.
|
static void |
append(java.nio.file.Path self,
java.io.InputStream stream)
Append binary data to the file.
|
static void |
append(java.nio.file.Path self,
java.lang.Object text)
Append the text at the end of the Path.
|
static void |
append(java.nio.file.Path self,
java.lang.Object text,
java.lang.String charset)
Append the text at the end of the Path, using a specified encoding.
|
static void |
append(java.nio.file.Path file,
java.io.Reader reader)
Append the text supplied by the Writer at the end of the File.
|
static void |
append(java.nio.file.Path file,
java.io.Reader reader,
java.lang.String charset)
Append the text supplied by the Reader at the end of the File, using a specified encoding.
|
static void |
append(java.nio.file.Path file,
java.io.Writer writer)
Append the text supplied by the Writer at the end of the File.
|
static void |
append(java.nio.file.Path file,
java.io.Writer writer,
java.lang.String charset)
Append the text supplied by the Writer at the end of the File, using a specified encoding.
|
static <T> T |
asType(java.nio.file.Path path,
java.lang.Class<T> c)
Converts this Path to a
Writable or delegates to default
DefaultGroovyMethods.asType(Object, Class) . |
static java.nio.file.Path |
asWritable(java.nio.file.Path self)
Converts this Path to a
Writable . |
static java.nio.file.Path |
asWritable(java.nio.file.Path self,
java.lang.String encoding)
Allows a file to return a Writable implementation that can output itself
to a Writer stream.
|
static boolean |
deleteDir(java.nio.file.Path self)
Deletes a directory with all contained files and subdirectories.
|
static void |
eachByte(java.nio.file.Path self,
Closure closure)
Traverse through each byte of this Path
|
static void |
eachByte(java.nio.file.Path self,
int bufferLen,
Closure closure)
Traverse through the bytes of this Path, bufferLen bytes at a time.
|
static void |
eachDir(java.nio.file.Path self,
Closure closure)
Invokes the closure for each subdirectory in this directory,
ignoring regular files.
|
static void |
eachDirMatch(java.nio.file.Path self,
java.lang.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. |
static void |
eachDirRecurse(java.nio.file.Path self,
Closure closure)
Recursively processes each descendant subdirectory in this directory.
|
static void |
eachFile(java.nio.file.Path self,
Closure closure)
Invokes the closure for each 'child' file in this 'parent' folder/directory.
|
static void |
eachFile(java.nio.file.Path self,
FileType fileType,
Closure closure)
Invokes the closure for each 'child' file in this 'parent' folder/directory.
|
static void |
eachFileMatch(java.nio.file.Path self,
FileType fileType,
java.lang.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(Object, Object) method to determine if a match occurs. |
static void |
eachFileMatch(java.nio.file.Path self,
java.lang.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(Object, Object) method to determine if a match occurs. |
static void |
eachFileRecurse(java.nio.file.Path self,
Closure closure)
Processes each descendant file in this directory and any sub-directories.
|
static void |
eachFileRecurse(java.nio.file.Path self,
FileType fileType,
Closure closure)
Processes each descendant file in this directory and any sub-directories.
|
static <T> T |
eachLine(java.nio.file.Path self,
Closure<T> closure)
Iterates through this path line by line.
|
static <T> T |
eachLine(java.nio.file.Path self,
int firstLine,
Closure<T> closure)
Iterates through this file line by line.
|
static <T> T |
eachLine(java.nio.file.Path self,
java.lang.String charset,
Closure<T> closure)
Iterates through this file line by line.
|
static <T> T |
eachLine(java.nio.file.Path self,
java.lang.String charset,
int firstLine,
Closure<T> closure)
Iterates through this file line by line.
|
static void |
eachObject(java.nio.file.Path self,
Closure closure)
Iterates through the given file object by object.
|
static Writable |
filterLine(java.nio.file.Path self,
Closure closure)
Filters the lines of a Path and creates a Writable in return to
stream the filtered lines.
|
static Writable |
filterLine(java.nio.file.Path self,
java.lang.String charset,
Closure closure)
Filters the lines of a Path and creates a Writable in return to
stream the filtered lines.
|
static void |
filterLine(java.nio.file.Path self,
java.io.Writer writer,
Closure closure)
Filter the lines from this Path, and write them to the given writer based
on the given closure predicate.
|
static void |
filterLine(java.nio.file.Path self,
java.io.Writer writer,
java.lang.String charset,
Closure closure)
Filter the lines from this Path, and write them to the given writer based
on the given closure predicate.
|
static byte[] |
getBytes(java.nio.file.Path self)
Read the content of the Path and returns it as a byte[].
|
static java.lang.String |
getText(java.nio.file.Path self)
Read the content of the Path and returns it as a String.
|
static java.lang.String |
getText(java.nio.file.Path self,
java.lang.String charset)
Read the content of the Path using the specified encoding and return it
as a String.
|
static java.nio.file.Path |
leftShift(java.nio.file.Path self,
byte[] bytes)
Write bytes to a Path.
|
static java.nio.file.Path |
leftShift(java.nio.file.Path path,
java.io.InputStream data)
Append binary data to the file.
|
static java.nio.file.Path |
leftShift(java.nio.file.Path self,
java.lang.Object text)
Write the text to the Path.
|
static java.io.DataInputStream |
newDataInputStream(java.nio.file.Path self)
Create a data input stream for this file
|
static java.io.DataOutputStream |
newDataOutputStream(java.nio.file.Path self)
Creates a new data output stream for this file.
|
static java.io.BufferedInputStream |
newInputStream(java.nio.file.Path self)
Creates a buffered input stream for this file.
|
static java.io.ObjectInputStream |
newObjectInputStream(java.nio.file.Path self)
Create an object input stream for this file.
|
static java.io.ObjectInputStream |
newObjectInputStream(java.nio.file.Path self,
java.lang.ClassLoader classLoader)
Create an object input stream for this path using the given class loader.
|
static java.io.ObjectOutputStream |
newObjectOutputStream(java.nio.file.Path self)
Create an object output stream for this path.
|
static java.io.BufferedOutputStream |
newOutputStream(java.nio.file.Path self)
Create a buffered output stream for this file.
|
static java.io.PrintWriter |
newPrintWriter(java.nio.file.Path self)
Create a new PrintWriter for this file.
|
static java.io.PrintWriter |
newPrintWriter(java.nio.file.Path self,
java.lang.String charset)
Create a new PrintWriter for this file, using specified
charset.
|
static java.io.BufferedReader |
newReader(java.nio.file.Path self)
Create a buffered reader for this file.
|
static java.io.BufferedReader |
newReader(java.nio.file.Path self,
java.lang.String charset)
Create a buffered reader for this file, using the specified
charset as the encoding.
|
static java.io.BufferedWriter |
newWriter(java.nio.file.Path self)
Create a buffered writer for this file.
|
static java.io.BufferedWriter |
newWriter(java.nio.file.Path self,
boolean append)
Creates a buffered writer for this file, optionally appending to the
existing file content.
|
static java.io.BufferedWriter |
newWriter(java.nio.file.Path self,
java.lang.String charset)
Creates a buffered writer for this file, writing data using the given
encoding.
|
static java.io.BufferedWriter |
newWriter(java.nio.file.Path self,
java.lang.String charset,
boolean append)
Helper method to create a buffered writer for a file.
|
static byte[] |
readBytes(java.nio.file.Path self)
Reads the content of the file into a byte array.
|
static java.util.List<java.lang.String> |
readLines(java.nio.file.Path self)
Reads the file into a list of Strings, with one item for each line.
|
static java.util.List<java.lang.String> |
readLines(java.nio.file.Path self,
java.lang.String charset)
Reads the file into a list of Strings, with one item for each line.
|
static boolean |
renameTo(java.nio.file.Path self,
java.lang.String newPathName)
Renames a file.
|
static boolean |
renameTo(java.nio.file.Path self,
java.net.URI newPathName)
Renames a file.
|
static void |
setBytes(java.nio.file.Path self,
byte[] bytes)
Write the bytes from the byte array to the Path.
|
static void |
setText(java.nio.file.Path self,
java.lang.String text)
Synonym for write(text) allowing file.text = 'foo'.
|
static void |
setText(java.nio.file.Path self,
java.lang.String text,
java.lang.String charset)
Synonym for write(text, charset) allowing:
|
static long |
size(java.nio.file.Path self)
Provide the standard Groovy
size() method for Path . |
static <T> T |
splitEachLine(java.nio.file.Path self,
java.util.regex.Pattern pattern,
Closure<T> closure)
Iterates through this file line by line, splitting each line using
the given separator Pattern.
|
static <T> T |
splitEachLine(java.nio.file.Path self,
java.util.regex.Pattern pattern,
java.lang.String charset,
Closure<T> closure)
Iterates through this file line by line, splitting each line using
the given regex separator Pattern.
|
static <T> T |
splitEachLine(java.nio.file.Path self,
java.lang.String regex,
Closure<T> closure)
Iterates through this file line by line, splitting each line using
the given regex separator.
|
static <T> T |
splitEachLine(java.nio.file.Path self,
java.lang.String regex,
java.lang.String charset,
Closure<T> closure)
Iterates through this file line by line, splitting each line using
the given regex separator.
|
static void |
traverse(java.nio.file.Path self,
Closure closure)
Processes each descendant file in this directory and any sub-directories.
|
static void |
traverse(java.nio.file.Path self,
java.util.Map<java.lang.String,java.lang.Object> options)
Invokes the closure specified with key 'visit' in the options Map
for each descendant file in this directory tree.
|
static void |
traverse(java.nio.file.Path self,
java.util.Map<java.lang.String,java.lang.Object> options,
Closure closure)
Processes each descendant file in this directory and any sub-directories.
|
static <T> T |
withCloseable(java.io.Closeable self,
Closure<T> action)
Deprecated.
|
static <T> T |
withDataInputStream(java.nio.file.Path self,
Closure<T> closure)
Create a new DataInputStream for this file and passes it into the closure.
|
static <T> T |
withDataOutputStream(java.nio.file.Path self,
Closure<T> closure)
Create a new DataOutputStream for this file and passes it into the closure.
|
static java.lang.Object |
withInputStream(java.nio.file.Path self,
Closure closure)
Create a new InputStream for this file and passes it into the closure.
|
static <T> T |
withObjectInputStream(java.nio.file.Path self,
java.lang.ClassLoader classLoader,
Closure<T> closure)
Create a new ObjectInputStream for this file associated with the given class loader and pass it to the closure.
|
static <T> T |
withObjectInputStream(java.nio.file.Path path,
Closure<T> closure)
Create a new ObjectInputStream for this file and pass it to the closure.
|
static <T> T |
withObjectOutputStream(java.nio.file.Path self,
Closure<T> closure)
Create a new ObjectOutputStream for this path and then pass it to the
closure.
|
static java.lang.Object |
withOutputStream(java.nio.file.Path self,
Closure closure)
Creates a new OutputStream for this file and passes it into the closure.
|
static <T> T |
withPrintWriter(java.nio.file.Path self,
Closure<T> closure)
Create a new PrintWriter for this file which is then
passed it into the given closure.
|
static <T> T |
withPrintWriter(java.nio.file.Path self,
java.lang.String charset,
Closure<T> closure)
Create a new PrintWriter with a specified charset for
this file.
|
static <T> T |
withReader(java.nio.file.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.
|
static <T> T |
withReader(java.nio.file.Path self,
java.lang.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.
|
static <T> T |
withWriter(java.nio.file.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.
|
static <T> T |
withWriter(java.nio.file.Path self,
java.lang.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.
|
static <T> T |
withWriterAppend(java.nio.file.Path self,
Closure<T> closure)
Create a new BufferedWriter for this file in append mode.
|
static <T> T |
withWriterAppend(java.nio.file.Path self,
java.lang.String charset,
Closure<T> closure)
Create a new BufferedWriter which will append to this
file.
|
static void |
write(java.nio.file.Path self,
java.lang.String text)
Write the text to the Path.
|
static void |
write(java.nio.file.Path self,
java.lang.String text,
java.lang.String charset)
Write the text to the Path, using the specified encoding.
|
cloneSimilarCollection, cloneSimilarMap, closeQuietly, closeWithWarning, createSimilarArray, createSimilarCollection, createSimilarCollection, createSimilarCollection, createSimilarList, createSimilarMap, createSimilarOrDefaultCollection, createSimilarSet, normaliseIndex, sameType, subListBorders, subListBorders
public static long size(java.nio.file.Path self) throws java.io.IOException
size()
method for Path
.self
- a Path
objectjava.io.IOException
public static java.io.ObjectOutputStream newObjectOutputStream(java.nio.file.Path self) throws java.io.IOException
self
- a Path
objectjava.io.IOException
- if an IOException occurs.public static <T> T withObjectOutputStream(java.nio.file.Path self, Closure<T> closure) throws java.io.IOException
self
- a Pathclosure
- a closurejava.io.IOException
- if an IOException occurs.IOGroovyMethods.withStream(java.io.OutputStream, groovy.lang.Closure)
public static java.io.ObjectInputStream newObjectInputStream(java.nio.file.Path self) throws java.io.IOException
self
- a Path
objectjava.io.IOException
- if an IOException occurs.public static java.io.ObjectInputStream newObjectInputStream(java.nio.file.Path self, java.lang.ClassLoader classLoader) throws java.io.IOException
self
- a Path
objectclassLoader
- the class loader to use when loading the classjava.io.IOException
- if an IOException occurs.public static void eachObject(java.nio.file.Path self, Closure closure) throws java.io.IOException, java.lang.ClassNotFoundException
self
- a Path
objectclosure
- a closurejava.io.IOException
- if an IOException occurs.java.lang.ClassNotFoundException
- if the class is not found.IOGroovyMethods.eachObject(java.io.ObjectInputStream, groovy.lang.Closure)
public static <T> T withObjectInputStream(java.nio.file.Path path, Closure<T> closure) throws java.io.IOException
path
- a Pathclosure
- a closurejava.io.IOException
- if an IOException occurs.IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
public static <T> T withObjectInputStream(java.nio.file.Path self, java.lang.ClassLoader classLoader, Closure<T> closure) throws java.io.IOException
self
- a PathclassLoader
- the class loader to use when loading the classclosure
- a closurejava.io.IOException
- if an IOException occurs.IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
public static <T> T eachLine(java.nio.file.Path self, Closure<T> closure) throws java.io.IOException
self
- a Pathclosure
- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)java.io.IOException
- if an IOException occurs.eachLine(Path, int, groovy.lang.Closure)
public static <T> T eachLine(java.nio.file.Path self, java.lang.String charset, Closure<T> closure) throws java.io.IOException
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)java.io.IOException
- if an IOException occurs.eachLine(Path, String, int, groovy.lang.Closure)
public static <T> T eachLine(java.nio.file.Path self, int firstLine, Closure<T> closure) throws java.io.IOException
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)java.io.IOException
- if an IOException occurs.IOGroovyMethods.eachLine(java.io.Reader, int, groovy.lang.Closure)
public static <T> T eachLine(java.nio.file.Path self, java.lang.String charset, int firstLine, Closure<T> closure) throws java.io.IOException
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)java.io.IOException
- if an IOException occurs.IOGroovyMethods.eachLine(java.io.Reader, int, groovy.lang.Closure)
public static <T> T splitEachLine(java.nio.file.Path self, java.lang.String regex, Closure<T> closure) throws java.io.IOException
self
- a Pathregex
- the delimiting regular expressionclosure
- a closurejava.io.IOException
- if an IOException occurs.java.util.regex.PatternSyntaxException
- if the regular expression's syntax is invalidIOGroovyMethods.splitEachLine(java.io.Reader, String, groovy.lang.Closure)
public static <T> T splitEachLine(java.nio.file.Path self, java.util.regex.Pattern pattern, Closure<T> closure) throws java.io.IOException
self
- a Pathpattern
- the regular expression Pattern for the delimiterclosure
- a closurejava.io.IOException
- if an IOException occurs.IOGroovyMethods.splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
public static <T> T splitEachLine(java.nio.file.Path self, java.lang.String regex, java.lang.String charset, Closure<T> closure) throws java.io.IOException
self
- a Pathregex
- the delimiting regular expressioncharset
- opens the file with a specified charsetclosure
- a closurejava.io.IOException
- if an IOException occurs.java.util.regex.PatternSyntaxException
- if the regular expression's syntax is invalidIOGroovyMethods.splitEachLine(java.io.Reader, String, groovy.lang.Closure)
public static <T> T splitEachLine(java.nio.file.Path self, java.util.regex.Pattern pattern, java.lang.String charset, Closure<T> closure) throws java.io.IOException
self
- a Pathpattern
- the regular expression Pattern for the delimitercharset
- opens the file with a specified charsetclosure
- a closurejava.io.IOException
- if an IOException occurs.IOGroovyMethods.splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
public static java.util.List<java.lang.String> readLines(java.nio.file.Path self) throws java.io.IOException
self
- a Pathjava.io.IOException
- if an IOException occurs.IOGroovyMethods.readLines(java.io.Reader)
public static java.util.List<java.lang.String> readLines(java.nio.file.Path self, java.lang.String charset) throws java.io.IOException
self
- a Pathcharset
- opens the file with a specified charsetjava.io.IOException
- if an IOException occurs.IOGroovyMethods.readLines(java.io.Reader)
public static java.lang.String getText(java.nio.file.Path self, java.lang.String charset) throws java.io.IOException
self
- the file whose content we want to readcharset
- the charset used to read the content of the filejava.io.IOException
- if an IOException occurs.public static java.lang.String getText(java.nio.file.Path self) throws java.io.IOException
self
- the file whose content we want to readjava.io.IOException
- if an IOException occurs.public static byte[] getBytes(java.nio.file.Path self) throws java.io.IOException
self
- the file whose content we want to readjava.io.IOException
- if an IOException occurs.public static void setBytes(java.nio.file.Path self, byte[] bytes) throws java.io.IOException
self
- the file to write tobytes
- the byte[] to write to the filejava.io.IOException
- if an IOException occurs.public static void write(java.nio.file.Path self, java.lang.String text) throws java.io.IOException
self
- a Pathtext
- the text to write to the Pathjava.io.IOException
- if an IOException occurs.public static void setText(java.nio.file.Path self, java.lang.String text) throws java.io.IOException
self
- a Pathtext
- the text to write to the Pathjava.io.IOException
- if an IOException occurs.write(Path, String)
public static void setText(java.nio.file.Path self, java.lang.String text, java.lang.String charset) throws java.io.IOException
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 Pathjava.io.IOException
- if an IOException occurs.write(Path, String, String)
public static java.nio.file.Path leftShift(java.nio.file.Path self, java.lang.Object text) throws java.io.IOException
self
- a Pathtext
- the text to write to the Pathjava.io.IOException
- if an IOException occurs.public static java.nio.file.Path leftShift(java.nio.file.Path self, byte[] bytes) throws java.io.IOException
self
- a Pathbytes
- the byte array to append to the end of the Pathjava.io.IOException
- if an IOException occurs.public static java.nio.file.Path leftShift(java.nio.file.Path path, java.io.InputStream data) throws java.io.IOException
append(Path, java.io.InputStream)
path
- a Pathdata
- an InputStream of data to write to the filejava.io.IOException
- if an IOException occurs.public static void write(java.nio.file.Path self, java.lang.String text, java.lang.String charset) throws java.io.IOException
self
- a Pathtext
- the text to write to the Pathcharset
- the charset usedjava.io.IOException
- if an IOException occurs.public static void append(java.nio.file.Path self, java.lang.Object text) throws java.io.IOException
self
- a Pathtext
- the text to append at the end of the Pathjava.io.IOException
- if an IOException occurs.public static void append(java.nio.file.Path file, java.io.Reader reader) throws java.io.IOException
file
- a Pathreader
- the Reader supplying the text to append at the end of the Filejava.io.IOException
- if an IOException occurs.public static void append(java.nio.file.Path file, java.io.Writer writer) throws java.io.IOException
file
- a Filewriter
- the Writer supplying the text to append at the end of the Filejava.io.IOException
- if an IOException occurs.public static void append(java.nio.file.Path self, byte[] bytes) throws java.io.IOException
self
- a Pathbytes
- the byte array to append to the end of the Pathjava.io.IOException
- if an IOException occurs.public static void append(java.nio.file.Path self, java.io.InputStream stream) throws java.io.IOException
self
- a Pathstream
- stream to read data from.java.io.IOException
- if an IOException occurs.public static void append(java.nio.file.Path self, java.lang.Object text, java.lang.String charset) throws java.io.IOException
self
- a Pathtext
- the text to append at the end of the Pathcharset
- the charset usedjava.io.IOException
- if an IOException occurs.public static void append(java.nio.file.Path file, java.io.Writer writer, java.lang.String charset) throws java.io.IOException
file
- a Filewriter
- the Writer supplying the text to append at the end of the Filecharset
- the charset usedjava.io.IOException
- if an IOException occurs.public static void append(java.nio.file.Path file, java.io.Reader reader, java.lang.String charset) throws java.io.IOException
file
- a Filereader
- the Reader supplying the text to append at the end of the Filecharset
- the charset usedjava.io.IOException
- if an IOException occurs.public static void eachFile(java.nio.file.Path self, FileType fileType, Closure closure) throws java.io.IOException
self
- a Path (that happens to be a folder/directory)fileType
- if normal files or directories or both should be processedclosure
- the closure to invokejava.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directoryjava.io.IOException
public static void eachFile(java.nio.file.Path self, Closure closure) throws java.io.IOException
self
- a Path (that happens to be a folder/directory)closure
- a closure (the parameter is the Path for the 'child' file)java.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directoryjava.io.IOException
eachFile(Path, groovy.io.FileType, groovy.lang.Closure)
public static void eachDir(java.nio.file.Path self, Closure closure) throws java.io.IOException
self
- a Path (that happens to be a folder/directory)closure
- a closure (the parameter is the Path for the subdirectory file)java.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directoryjava.io.IOException
eachFile(Path, groovy.io.FileType, groovy.lang.Closure)
public static void eachFileRecurse(java.nio.file.Path self, FileType fileType, Closure closure) throws java.io.IOException
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 filejava.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directoryjava.io.IOException
public static void traverse(java.nio.file.Path self, java.util.Map<java.lang.String,java.lang.Object> options, Closure closure) throws java.io.IOException
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:FileType
enum to determine if normal files or directories or both are processedClosure
run before each directory is processed and optionally returning a FileVisitResult
value
which can be used to control subsequent processing.Closure
run after each directory is processed and optionally returning a FileVisitResult
value
which can be used to control subsequent processing.FileType.FILES
)DefaultGroovyMethods.isCase(Object, Object)
method). If set,
only files/dirs which match are candidates for visiting.DefaultGroovyMethods.isCase(Object, Object)
method). If set,
only files/dirs which match are candidates for visiting. (Must not be set if 'filter' is set)DefaultGroovyMethods.isCase(Object, Object)
method).
If set, any candidates which match won't be visited.DefaultGroovyMethods.isCase(Object, Object)
method).
If set, any candidates which match won't be visited. (Must not be set if 'excludeFilter' is set)Closure
which if set causes the files and subdirectories for each directory to be processed in sorted order.
Note that even when processing only files, the order of visited subdirectories will be affected by this parameter.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 processingjava.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directory or illegal filter combinations are suppliedjava.io.IOException
DefaultGroovyMethods.sort(java.util.Collection, groovy.lang.Closure)
,
FileVisitResult
,
FileType
public static void traverse(java.nio.file.Path self, Closure closure) throws java.io.IOException
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 processingjava.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directoryjava.io.IOException
traverse(Path, java.util.Map, groovy.lang.Closure)
public static void traverse(java.nio.file.Path self, java.util.Map<java.lang.String,java.lang.Object> options) throws java.io.IOException
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 behaviorjava.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directory or illegal filter combinations are suppliedjava.io.IOException
traverse(Path, java.util.Map, groovy.lang.Closure)
public static void eachFileRecurse(java.nio.file.Path self, Closure closure) throws java.io.IOException
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 Closurejava.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directoryjava.io.IOException
eachFileRecurse(Path, groovy.io.FileType, groovy.lang.Closure)
public static void eachDirRecurse(java.nio.file.Path self, Closure closure) throws java.io.IOException
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 closurejava.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directoryjava.io.IOException
eachFileRecurse(Path, groovy.io.FileType, groovy.lang.Closure)
public static void eachFileMatch(java.nio.file.Path self, FileType fileType, java.lang.Object nameFilter, Closure closure) throws java.io.IOException
DefaultGroovyMethods.isCase(Object, Object)
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(Object, Object)
method)closure
- the closure to invokejava.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directoryjava.io.IOException
public static void eachFileMatch(java.nio.file.Path self, java.lang.Object nameFilter, Closure closure) throws java.io.IOException
DefaultGroovyMethods.isCase(Object, Object)
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(Object, Object)
method)closure
- the closure to invokejava.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directoryjava.io.IOException
eachFileMatch(Path, groovy.io.FileType, Object, groovy.lang.Closure)
public static void eachDirMatch(java.nio.file.Path self, java.lang.Object nameFilter, Closure closure) throws java.io.IOException
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(Object, Object)
method)closure
- the closure to invokejava.io.FileNotFoundException
- if the given directory does not existjava.lang.IllegalArgumentException
- if the provided Path object does not represent a directoryjava.io.IOException
eachFileMatch(Path, groovy.io.FileType, Object, groovy.lang.Closure)
public static boolean deleteDir(java.nio.file.Path self)
The method returns
self
- a Pathpublic static boolean renameTo(java.nio.file.Path self, java.lang.String newPathName)
self
- a PathnewPathName
- The new pathname for the named filetrue
if and only if the renaming succeeded;
false
otherwisepublic static boolean renameTo(java.nio.file.Path self, java.net.URI newPathName)
self
- a PathnewPathName
- The new target path specified as a URI objecttrue
if and only if the renaming succeeded;
false
otherwisepublic static java.nio.file.Path asWritable(java.nio.file.Path self)
Writable
.self
- a Pathpublic static <T> T asType(java.nio.file.Path path, java.lang.Class<T> c)
Writable
or delegates to default
DefaultGroovyMethods.asType(Object, Class)
.path
- a Pathc
- the desired classpublic static java.nio.file.Path asWritable(java.nio.file.Path self, java.lang.String encoding)
self
- a Pathencoding
- the encoding to be used when reading the file's contentspublic static java.io.BufferedReader newReader(java.nio.file.Path self) throws java.io.IOException
self
- a Pathjava.io.IOException
- if an IOException occurs.public static java.io.BufferedReader newReader(java.nio.file.Path self, java.lang.String charset) throws java.io.IOException
self
- a Pathcharset
- the charset for this Pathjava.io.FileNotFoundException
- if the Path was not foundjava.io.UnsupportedEncodingException
- if the encoding specified is not supportedjava.io.IOException
public static <T> T withReader(java.nio.file.Path self, Closure<T> closure) throws java.io.IOException
self
- a file objectclosure
- a closurejava.io.IOException
- if an IOException occurs.public static <T> T withReader(java.nio.file.Path self, java.lang.String charset, Closure<T> closure) throws java.io.IOException
self
- a file objectcharset
- the charset for this input streamclosure
- a closurejava.io.IOException
- if an IOException occurs.public static java.io.BufferedOutputStream newOutputStream(java.nio.file.Path self) throws java.io.IOException
self
- a file objectjava.io.IOException
- if an IOException occurs.public static java.io.DataOutputStream newDataOutputStream(java.nio.file.Path self) throws java.io.IOException
self
- a file objectjava.io.IOException
- if an IOException occurs.public static java.lang.Object withOutputStream(java.nio.file.Path self, Closure closure) throws java.io.IOException
self
- a Pathclosure
- a closurejava.io.IOException
- if an IOException occurs.IOGroovyMethods.withStream(java.io.OutputStream, groovy.lang.Closure)
public static java.lang.Object withInputStream(java.nio.file.Path self, Closure closure) throws java.io.IOException
self
- a Pathclosure
- a closurejava.io.IOException
- if an IOException occurs.IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
public static <T> T withDataOutputStream(java.nio.file.Path self, Closure<T> closure) throws java.io.IOException
self
- a Pathclosure
- a closurejava.io.IOException
- if an IOException occurs.IOGroovyMethods.withStream(java.io.OutputStream, groovy.lang.Closure)
public static <T> T withDataInputStream(java.nio.file.Path self, Closure<T> closure) throws java.io.IOException
self
- a Pathclosure
- a closurejava.io.IOException
- if an IOException occurs.IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
public static java.io.BufferedWriter newWriter(java.nio.file.Path self) throws java.io.IOException
self
- a Pathjava.io.IOException
- if an IOException occurs.public static java.io.BufferedWriter newWriter(java.nio.file.Path self, boolean append) throws java.io.IOException
self
- a Pathappend
- true if data should be appended to the filejava.io.IOException
- if an IOException occurs.public static java.io.BufferedWriter newWriter(java.nio.file.Path self, java.lang.String charset, boolean append) throws java.io.IOException
self
- a Pathcharset
- the name of the encoding used to write in this fileappend
- true if in append modejava.io.IOException
- if an IOException occurs.public static java.io.BufferedWriter newWriter(java.nio.file.Path self, java.lang.String charset) throws java.io.IOException
self
- a Pathcharset
- the name of the encoding used to write in this filejava.io.IOException
- if an IOException occurs.public static <T> T withWriter(java.nio.file.Path self, Closure<T> closure) throws java.io.IOException
self
- a Pathclosure
- a closurejava.io.IOException
- if an IOException occurs.public static <T> T withWriter(java.nio.file.Path self, java.lang.String charset, Closure<T> closure) throws java.io.IOException
self
- a Pathcharset
- the charset usedclosure
- a closurejava.io.IOException
- if an IOException occurs.public static <T> T withWriterAppend(java.nio.file.Path self, java.lang.String charset, Closure<T> closure) throws java.io.IOException
self
- a Pathcharset
- the charset usedclosure
- a closurejava.io.IOException
- if an IOException occurs.public static <T> T withWriterAppend(java.nio.file.Path self, Closure<T> closure) throws java.io.IOException
self
- a Pathclosure
- a closurejava.io.IOException
- if an IOException occurs.public static java.io.PrintWriter newPrintWriter(java.nio.file.Path self) throws java.io.IOException
self
- a Pathjava.io.IOException
- if an IOException occurs.public static java.io.PrintWriter newPrintWriter(java.nio.file.Path self, java.lang.String charset) throws java.io.IOException
self
- a Pathcharset
- the charsetjava.io.IOException
- if an IOException occurs.public static <T> T withPrintWriter(java.nio.file.Path self, Closure<T> closure) throws java.io.IOException
self
- a Pathclosure
- the closure to invoke with the PrintWriterjava.io.IOException
- if an IOException occurs.public static <T> T withPrintWriter(java.nio.file.Path self, java.lang.String charset, Closure<T> closure) throws java.io.IOException
self
- a Pathcharset
- the charsetclosure
- the closure to invoke with the PrintWriterjava.io.IOException
- if an IOException occurs.public static java.io.BufferedInputStream newInputStream(java.nio.file.Path self) throws java.io.IOException
self
- a Pathjava.io.FileNotFoundException
- if the file is not found.java.io.IOException
public static java.io.DataInputStream newDataInputStream(java.nio.file.Path self) throws java.io.IOException
self
- a Pathjava.io.FileNotFoundException
- if the file is not found.java.io.IOException
public static void eachByte(java.nio.file.Path self, Closure closure) throws java.io.IOException
self
- a Pathclosure
- a closurejava.io.IOException
- if an IOException occurs.IOGroovyMethods.eachByte(java.io.InputStream, groovy.lang.Closure)
public static void eachByte(java.nio.file.Path self, int bufferLen, Closure closure) throws java.io.IOException
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.java.io.IOException
- if an IOException occurs.IOGroovyMethods.eachByte(java.io.InputStream, int, groovy.lang.Closure)
public static Writable filterLine(java.nio.file.Path self, Closure closure) throws java.io.IOException
self
- a Pathclosure
- a closure which returns a boolean indicating to filter
the line or notjava.io.IOException
- if self
is not readableIOGroovyMethods.filterLine(java.io.Reader, groovy.lang.Closure)
public static Writable filterLine(java.nio.file.Path self, java.lang.String charset, Closure closure) throws java.io.IOException
self
- a Pathcharset
- opens the file with a specified charsetclosure
- a closure which returns a boolean indicating to filter
the line or notjava.io.IOException
- if an IOException occursIOGroovyMethods.filterLine(java.io.Reader, groovy.lang.Closure)
public static void filterLine(java.nio.file.Path self, java.io.Writer writer, Closure closure) throws java.io.IOException
self
- 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.java.io.IOException
- if self
is not readableIOGroovyMethods.filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
public static void filterLine(java.nio.file.Path self, java.io.Writer writer, java.lang.String charset, Closure closure) throws java.io.IOException
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.java.io.IOException
- if an IO error occursIOGroovyMethods.filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
public static byte[] readBytes(java.nio.file.Path self) throws java.io.IOException
self
- a Pathjava.io.IOException
- if an IOException occurs.@Deprecated public static <T> T withCloseable(java.io.Closeable self, Closure<T> action) throws java.io.IOException
java.io.IOException
IOGroovyMethods.withCloseable(java.io.Closeable, groovy.lang.Closure)