Package org.apache.groovy.nio.extensions
Class NioExtensions
java.lang.Object
org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport
org.apache.groovy.nio.extensions.NioExtensions
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.
 
 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 SummaryConstructors Constructor Description NioExtensions()
- 
Method SummaryModifier and Type Method Description static voidappend(java.nio.file.Path self, byte[] bytes)Append bytes to the end of a Path.static voidappend(java.nio.file.Path self, java.io.InputStream stream)Append binary data to the file.static voidappend(java.nio.file.Path file, java.io.Reader reader)Append the text supplied by the Writer at the end of the File without writing a BOM.static voidappend(java.nio.file.Path file, java.io.Reader reader, boolean writeBom)Append the text supplied by the Reader at the end of the File, using a specified encoding.static voidappend(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 without writing a BOM, using a specified encoding.static voidappend(java.nio.file.Path file, java.io.Reader reader, java.lang.String charset, boolean writeBom)Append the text supplied by the Reader at the end of the File, using a specified encoding.static voidappend(java.nio.file.Path file, java.io.Writer writer)Append the text supplied by the Writer at the end of the File without writing a BOM.static voidappend(java.nio.file.Path file, java.io.Writer writer, boolean writeBom)Append the text supplied by the Writer at the end of the File, using a specified encoding.static voidappend(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 without writing a BOM, using a specified encoding.static voidappend(java.nio.file.Path file, java.io.Writer writer, java.lang.String charset, boolean writeBom)Append the text supplied by the Writer at the end of the File, using a specified encoding.static voidappend(java.nio.file.Path self, java.lang.Object text)Append the text at the end of the Path without writing a BOM.static voidappend(java.nio.file.Path self, java.lang.Object text, boolean writeBom)Append the text at the end of the Path.static voidappend(java.nio.file.Path self, java.lang.Object text, java.lang.String charset)Append the text at the end of the Path without writing a BOM, using a specified encoding.static voidappend(java.nio.file.Path self, java.lang.Object text, java.lang.String charset, boolean writeBom)Append the text at the end of the Path, using a specified encoding.static <T> TasType(java.nio.file.Path path, java.lang.Class<T> c)Converts this Path to aWritableor delegates to defaultDefaultGroovyMethods.asType(Object, Class).static java.nio.file.PathasWritable(java.nio.file.Path self)Converts this Path to aWritable.static java.nio.file.PathasWritable(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 booleandeleteDir(java.nio.file.Path self)Deletes a directory with all contained files and subdirectories.static voideachByte(java.nio.file.Path self, int bufferLen, Closure closure)Traverse through the bytes of this Path, bufferLen bytes at a time.static voideachByte(java.nio.file.Path self, Closure closure)Traverse through each byte of this Pathstatic voideachDir(java.nio.file.Path self, Closure closure)Invokes the closure for each subdirectory in this directory, ignoring regular files.static voideachDirMatch(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 theDefaultGroovyMethods.isCase(java.lang.Object, java.lang.Object)method to determine if a match occurs.static voideachDirRecurse(java.nio.file.Path self, Closure closure)Recursively processes each descendant subdirectory in this directory.static voideachFile(java.nio.file.Path self, FileType fileType, Closure closure)Invokes the closure for each 'child' file in this 'parent' folder/directory.static voideachFile(java.nio.file.Path self, Closure closure)Invokes the closure for each 'child' file in this 'parent' folder/directory.static voideachFileMatch(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 theDefaultGroovyMethods.isCase(Object, Object)method to determine if a match occurs.static voideachFileMatch(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 theDefaultGroovyMethods.isCase(Object, Object)method to determine if a match occurs.static voideachFileRecurse(java.nio.file.Path self, FileType fileType, Closure closure)Processes each descendant file in this directory and any sub-directories.static voideachFileRecurse(java.nio.file.Path self, Closure closure)Processes each descendant file in this directory and any sub-directories.static <T> TeachLine(java.nio.file.Path self, int firstLine, Closure<T> closure)Iterates through this file line by line.static <T> TeachLine(java.nio.file.Path self, Closure<T> closure)Iterates through this path line by line.static <T> TeachLine(java.nio.file.Path self, java.lang.String charset, int firstLine, Closure<T> closure)Iterates through this file line by line.static <T> TeachLine(java.nio.file.Path self, java.lang.String charset, Closure<T> closure)Iterates through this file line by line.static voideachObject(java.nio.file.Path self, Closure closure)Iterates through the given file object by object.static WritablefilterLine(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 voidfilterLine(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 voidfilterLine(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 WritablefilterLine(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 byte[]getBytes(java.nio.file.Path self)Read the content of the Path and returns it as a byte[].static java.lang.StringgetText(java.nio.file.Path self)Read the content of the Path and returns it as a String.static java.lang.StringgetText(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.PathleftShift(java.nio.file.Path self, byte[] bytes)Write bytes to a Path.static java.nio.file.PathleftShift(java.nio.file.Path path, java.io.InputStream data)Append binary data to the file.static java.nio.file.PathleftShift(java.nio.file.Path self, java.lang.Object text)Write the text to the Path.static java.io.DataInputStreamnewDataInputStream(java.nio.file.Path self)Create a data input stream for this filestatic java.io.DataOutputStreamnewDataOutputStream(java.nio.file.Path self)Creates a new data output stream for this file.static java.io.BufferedInputStreamnewInputStream(java.nio.file.Path self)Creates a buffered input stream for this file.static java.io.ObjectInputStreamnewObjectInputStream(java.nio.file.Path self)Create an object input stream for this file.static java.io.ObjectInputStreamnewObjectInputStream(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.ObjectOutputStreamnewObjectOutputStream(java.nio.file.Path self)Create an object output stream for this path.static java.io.BufferedOutputStreamnewOutputStream(java.nio.file.Path self)Create a buffered output stream for this file.static java.io.PrintWriternewPrintWriter(java.nio.file.Path self)Create a new PrintWriter for this file.static java.io.PrintWriternewPrintWriter(java.nio.file.Path self, java.lang.String charset)Create a new PrintWriter for this file, using specified charset.static java.io.BufferedReadernewReader(java.nio.file.Path self)Create a buffered reader for this file.static java.io.BufferedReadernewReader(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.BufferedWriternewWriter(java.nio.file.Path self)Create a buffered writer for this file.static java.io.BufferedWriternewWriter(java.nio.file.Path self, boolean append)Creates a buffered writer for this file, optionally appending to the existing file content.static java.io.BufferedWriternewWriter(java.nio.file.Path self, java.lang.String charset)Creates a buffered writer for this file without writing a BOM, writing data using the given encoding.static java.io.BufferedWriternewWriter(java.nio.file.Path self, java.lang.String charset, boolean append)Helper method to create a buffered writer for a file without writing a BOM.static java.io.BufferedWriternewWriter(java.nio.file.Path self, java.lang.String charset, boolean append, boolean writeBom)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 booleanrenameTo(java.nio.file.Path self, java.lang.String newPathName)Renames a file.static booleanrenameTo(java.nio.file.Path self, java.net.URI newPathName)Renames a file.static voidsetBytes(java.nio.file.Path self, byte[] bytes)Write the bytes from the byte array to the Path.static voidsetText(java.nio.file.Path self, java.lang.String text)Synonym for write(text) allowing file.text = 'foo'.static voidsetText(java.nio.file.Path self, java.lang.String text, java.lang.String charset)Synonym for write(text, charset) allowing:static longsize(java.nio.file.Path self)Provide the standard Groovysize()method forPath.static <T> TsplitEachLine(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> TsplitEachLine(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 <T> TsplitEachLine(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> TsplitEachLine(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 voidtraverse(java.nio.file.Path self, Closure closure)Processes each descendant file in this directory and any sub-directories.static voidtraverse(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 voidtraverse(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> TwithCloseable(java.io.Closeable self, Closure<T> action)Deprecated.static <T> TwithDataInputStream(java.nio.file.Path self, Closure<T> closure)Create a new DataInputStream for this file and passes it into the closure.static <T> TwithDataOutputStream(java.nio.file.Path self, Closure<T> closure)Create a new DataOutputStream for this file and passes it into the closure.static <T> TwithInputStream(java.nio.file.Path self, Closure<T> closure)Create a new InputStream for this file and passes it into the closure.static <T> TwithObjectInputStream(java.nio.file.Path path, Closure<T> closure)Create a new ObjectInputStream for this file and pass it to the closure.static <T> TwithObjectInputStream(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> TwithObjectOutputStream(java.nio.file.Path self, Closure<T> closure)Create a new ObjectOutputStream for this path and then pass it to the closure.static <T> TwithOutputStream(java.nio.file.Path self, Closure<T> closure)Creates a new OutputStream for this file and passes it into the closure.static <T> TwithPrintWriter(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> TwithPrintWriter(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> TwithReader(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> TwithReader(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> TwithWriter(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> TwithWriter(java.nio.file.Path self, java.lang.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.static <T> TwithWriter(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> TwithWriterAppend(java.nio.file.Path self, Closure<T> closure)Create a new BufferedWriter for this file in append mode.static <T> TwithWriterAppend(java.nio.file.Path self, java.lang.String charset, boolean writeBom, Closure<T> closure)Create a new BufferedWriter which will append to this file.static <T> TwithWriterAppend(java.nio.file.Path self, java.lang.String charset, Closure<T> closure)Create a new BufferedWriter which will append to this file.static voidwrite(java.nio.file.Path self, java.lang.String text)Write the text to the Path without writing a BOM .static voidwrite(java.nio.file.Path self, java.lang.String text, boolean writeBom)Write the text to the Path.static voidwrite(java.nio.file.Path self, java.lang.String text, java.lang.String charset)Write the text to the Path without writing a BOM, using the specified encoding.static voidwrite(java.nio.file.Path self, java.lang.String text, java.lang.String charset, boolean writeBom)Write the text to the Path, using the specified encoding.Methods inherited from class org.codehaus.groovy.runtime.DefaultGroovyMethodsSupportcloneSimilarCollection, cloneSimilarMap, closeQuietly, closeWithWarning, createSimilarArray, createSimilarCollection, createSimilarCollection, createSimilarCollection, createSimilarList, createSimilarMap, createSimilarOrDefaultCollection, createSimilarQueue, createSimilarSet, normaliseIndex, sameType, subListBorders, subListBorders, writeUTF16BomIfRequired, writeUTF16BomIfRequired, writeUTF16BomIfRequired, writeUTF16BomIfRequiredMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
- 
Constructor Details- 
NioExtensionspublic NioExtensions()
 
- 
- 
Method Details- 
sizepublic static long size(java.nio.file.Path self) throws java.io.IOExceptionProvide the standard Groovysize()method forPath.- Parameters:
- self- a- Pathobject
- Returns:
- the file's size (length)
- Throws:
- java.io.IOException
- Since:
- 2.3.0
 
- 
newObjectOutputStreampublic static java.io.ObjectOutputStream newObjectOutputStream(java.nio.file.Path self) throws java.io.IOExceptionCreate an object output stream for this path.- Parameters:
- self- a- Pathobject
- Returns:
- an object output stream
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
withObjectOutputStreampublic static <T> T withObjectOutputStream(java.nio.file.Path self, Closure<T> closure) throws java.io.IOExceptionCreate a new ObjectOutputStream for this path and then pass it to the closure. This method ensures the stream is closed after the closure returns.- Parameters:
- self- a Path
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.withStream(java.io.OutputStream, groovy.lang.Closure)
 
- 
newObjectInputStreampublic static java.io.ObjectInputStream newObjectInputStream(java.nio.file.Path self) throws java.io.IOExceptionCreate an object input stream for this file.- Parameters:
- self- a- Pathobject
- Returns:
- an object input stream
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
newObjectInputStreampublic static java.io.ObjectInputStream newObjectInputStream(java.nio.file.Path self, java.lang.ClassLoader classLoader) throws java.io.IOExceptionCreate an object input stream for this path using the given class loader.- Parameters:
- self- a- Pathobject
- classLoader- the class loader to use when loading the class
- Returns:
- an object input stream
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
eachObjectpublic static void eachObject(java.nio.file.Path self, Closure closure) throws java.io.IOException, java.lang.ClassNotFoundExceptionIterates through the given file object by object.- Parameters:
- self- a- Pathobject
- closure- a closure
- Throws:
- java.io.IOException- if an IOException occurs.
- java.lang.ClassNotFoundException- if the class is not found.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.eachObject(java.io.ObjectInputStream, groovy.lang.Closure)
 
- 
withObjectInputStreampublic static <T> T withObjectInputStream(java.nio.file.Path path, Closure<T> closure) throws java.io.IOExceptionCreate a new ObjectInputStream for this file and pass it to the closure. This method ensures the stream is closed after the closure returns.- Parameters:
- path- a Path
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
 
- 
withObjectInputStreampublic static <T> T withObjectInputStream(java.nio.file.Path self, java.lang.ClassLoader classLoader, Closure<T> closure) throws java.io.IOExceptionCreate 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.- Parameters:
- self- a Path
- classLoader- the class loader to use when loading the class
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
 
- 
eachLinepublic static <T> T eachLine(java.nio.file.Path self, Closure<T> closure) throws java.io.IOExceptionIterates 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.- Parameters:
- self- a Path
- closure- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
- Returns:
- the last value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- eachLine(Path, int, groovy.lang.Closure)
 
- 
eachLinepublic static <T> T eachLine(java.nio.file.Path self, java.lang.String charset, Closure<T> closure) throws java.io.IOExceptionIterates 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.- Parameters:
- self- a Path
- charset- opens the file with a specified charset
- closure- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
- Returns:
- the last value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- eachLine(Path, String, int, groovy.lang.Closure)
 
- 
eachLinepublic static <T> T eachLine(java.nio.file.Path self, int firstLine, Closure<T> closure) throws java.io.IOExceptionIterates 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.- Parameters:
- self- a Path
- firstLine- 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)
- Returns:
- the last value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.eachLine(java.io.Reader, int, groovy.lang.Closure)
 
- 
eachLinepublic static <T> T eachLine(java.nio.file.Path self, java.lang.String charset, int firstLine, Closure<T> closure) throws java.io.IOExceptionIterates 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.- Parameters:
- self- a Path
- charset- opens the file with a specified charset
- firstLine- 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)
- Returns:
- the last value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.eachLine(java.io.Reader, int, groovy.lang.Closure)
 
- 
splitEachLinepublic static <T> T splitEachLine(java.nio.file.Path self, java.lang.String regex, Closure<T> closure) throws java.io.IOExceptionIterates 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.- Parameters:
- self- a Path
- regex- the delimiting regular expression
- closure- a closure
- Returns:
- the last value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- java.util.regex.PatternSyntaxException- if the regular expression's syntax is invalid
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.splitEachLine(java.io.Reader, String, groovy.lang.Closure)
 
- 
splitEachLinepublic static <T> T splitEachLine(java.nio.file.Path self, java.util.regex.Pattern pattern, Closure<T> closure) throws java.io.IOExceptionIterates 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.- Parameters:
- self- a Path
- pattern- the regular expression Pattern for the delimiter
- closure- a closure
- Returns:
- the last value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
 
- 
splitEachLinepublic static <T> T splitEachLine(java.nio.file.Path self, java.lang.String regex, java.lang.String charset, Closure<T> closure) throws java.io.IOExceptionIterates 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.- Parameters:
- self- a Path
- regex- the delimiting regular expression
- charset- opens the file with a specified charset
- closure- a closure
- Returns:
- the last value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- java.util.regex.PatternSyntaxException- if the regular expression's syntax is invalid
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.splitEachLine(java.io.Reader, String, groovy.lang.Closure)
 
- 
splitEachLinepublic static <T> T splitEachLine(java.nio.file.Path self, java.util.regex.Pattern pattern, java.lang.String charset, Closure<T> closure) throws java.io.IOExceptionIterates 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.- Parameters:
- self- a Path
- pattern- the regular expression Pattern for the delimiter
- charset- opens the file with a specified charset
- closure- a closure
- Returns:
- the last value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
 
- 
readLinespublic static java.util.List<java.lang.String> readLines(java.nio.file.Path self) throws java.io.IOExceptionReads the file into a list of Strings, with one item for each line.- Parameters:
- self- a Path
- Returns:
- a List of lines
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.readLines(java.io.Reader)
 
- 
readLinespublic static java.util.List<java.lang.String> readLines(java.nio.file.Path self, java.lang.String charset) throws java.io.IOExceptionReads the file into a list of Strings, with one item for each line.- Parameters:
- self- a Path
- charset- opens the file with a specified charset
- Returns:
- a List of lines
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.readLines(java.io.Reader)
 
- 
getTextpublic static java.lang.String getText(java.nio.file.Path self, java.lang.String charset) throws java.io.IOExceptionRead the content of the Path using the specified encoding and return it as a String.- Parameters:
- self- the file whose content we want to read
- charset- the charset used to read the content of the file
- Returns:
- a String containing the content of the file
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
getTextpublic static java.lang.String getText(java.nio.file.Path self) throws java.io.IOExceptionRead the content of the Path and returns it as a String.- Parameters:
- self- the file whose content we want to read
- Returns:
- a String containing the content of the file
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
getBytespublic static byte[] getBytes(java.nio.file.Path self) throws java.io.IOExceptionRead the content of the Path and returns it as a byte[].- Parameters:
- self- the file whose content we want to read
- Returns:
- a String containing the content of the file
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
setBytespublic static void setBytes(java.nio.file.Path self, byte[] bytes) throws java.io.IOExceptionWrite the bytes from the byte array to the Path.- Parameters:
- self- the file to write to
- bytes- the byte[] to write to the file
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
writepublic static void write(java.nio.file.Path self, java.lang.String text) throws java.io.IOExceptionWrite the text to the Path without writing a BOM .- Parameters:
- self- a Path
- text- the text to write to the Path
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
writepublic static void write(java.nio.file.Path self, java.lang.String text, boolean writeBom) throws java.io.IOExceptionWrite the text to the Path. If the default charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) andwriteBomistrue, the requisite byte order mark is written to the file before the text.- Parameters:
- self- a Path
- text- the text to write to the Path
- writeBom- whether to write the BOM
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
setTextpublic static void setText(java.nio.file.Path self, java.lang.String text) throws java.io.IOExceptionSynonym for write(text) allowing file.text = 'foo'.- Parameters:
- self- a Path
- text- the text to write to the Path
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- write(Path, String)
 
- 
setTextpublic static void setText(java.nio.file.Path self, java.lang.String text, java.lang.String charset) throws java.io.IOExceptionSynonym for write(text, charset) allowing:myFile.setText('some text', charset)or with some help fromExpandoMetaClass, you could do something like:myFile.metaClass.setText = { String s->delegate.setText(s, 'UTF-8') } myfile.text = 'some text'- Parameters:
- self- A Path
- charset- The charset used when writing to the file
- text- The text to write to the Path
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- write(Path, String, String)
 
- 
leftShiftpublic static java.nio.file.Path leftShift(java.nio.file.Path self, java.lang.Object text) throws java.io.IOExceptionWrite the text to the Path.- Parameters:
- self- a Path
- text- the text to write to the Path
- Returns:
- the original file
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
leftShiftpublic static java.nio.file.Path leftShift(java.nio.file.Path self, byte[] bytes) throws java.io.IOExceptionWrite bytes to a Path.- Parameters:
- self- a Path
- bytes- the byte array to append to the end of the Path
- Returns:
- the original file
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
leftShiftpublic static java.nio.file.Path leftShift(java.nio.file.Path path, java.io.InputStream data) throws java.io.IOExceptionAppend binary data to the file. Seeappend(Path, java.io.InputStream)- Parameters:
- path- a Path
- data- an InputStream of data to write to the file
- Returns:
- the file
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
writepublic static void write(java.nio.file.Path self, java.lang.String text, java.lang.String charset) throws java.io.IOExceptionWrite the text to the Path without writing a BOM, using the specified encoding.- Parameters:
- self- a Path
- text- the text to write to the Path
- charset- the charset used
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
writepublic static void write(java.nio.file.Path self, java.lang.String text, java.lang.String charset, boolean writeBom) throws java.io.IOExceptionWrite the text to the Path, using the specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) andwriteBomistrue, the requisite byte order mark is written to the file before the text.- Parameters:
- self- a Path
- text- the text to write to the Path
- charset- the charset used
- writeBom- whether to write a BOM
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
appendpublic static void append(java.nio.file.Path self, java.lang.Object text) throws java.io.IOExceptionAppend the text at the end of the Path without writing a BOM.- Parameters:
- self- a Path
- text- the text to append at the end of the Path
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
appendpublic static void append(java.nio.file.Path file, java.io.Reader reader) throws java.io.IOExceptionAppend the text supplied by the Writer at the end of the File without writing a BOM.- Parameters:
- file- a Path
- reader- the Reader supplying the text to append at the end of the File
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
appendpublic static void append(java.nio.file.Path file, java.io.Writer writer) throws java.io.IOExceptionAppend the text supplied by the Writer at the end of the File without writing a BOM.- Parameters:
- file- a File
- writer- the Writer supplying the text to append at the end of the File
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
appendpublic static void append(java.nio.file.Path self, byte[] bytes) throws java.io.IOExceptionAppend bytes to the end of a Path. It will not be interpreted as text.- Parameters:
- self- a Path
- bytes- the byte array to append to the end of the Path
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
appendpublic static void append(java.nio.file.Path self, java.io.InputStream stream) throws java.io.IOExceptionAppend binary data to the file. It will not be interpreted as text.- Parameters:
- self- a Path
- stream- stream to read data from.
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
appendpublic static void append(java.nio.file.Path self, java.lang.Object text, boolean writeBom) throws java.io.IOExceptionAppend the text at the end of the Path. If the default charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) andwriteBomistrue, the requisite byte order mark is written to the file before the text.- Parameters:
- self- a Path
- text- the text to append at the end of the Path
- writeBom- whether to write the BOM
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
appendpublic static void append(java.nio.file.Path self, java.lang.Object text, java.lang.String charset) throws java.io.IOExceptionAppend the text at the end of the Path without writing a BOM, using a specified encoding.- Parameters:
- self- a Path
- text- the text to append at the end of the Path
- charset- the charset used
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
appendpublic static void append(java.nio.file.Path self, java.lang.Object text, java.lang.String charset, boolean writeBom) throws java.io.IOExceptionAppend 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),writeBomistrue, and the file doesn't already exist, the requisite byte order mark is written to the file before the text is appended.- Parameters:
- self- a Path
- text- the text to append at the end of the Path
- charset- the charset used
- writeBom- whether to write the BOM
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
appendpublic static void append(java.nio.file.Path file, java.io.Writer writer, boolean writeBom) throws java.io.IOExceptionAppend 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),writeBomistrue, and the file doesn't already exist, the requisite byte order mark is written to the file before the text is appended.- Parameters:
- file- a File
- writer- the Writer supplying the text to append at the end of the File
- writeBom- whether to write the BOM
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
appendpublic static void append(java.nio.file.Path file, java.io.Writer writer, java.lang.String charset) throws java.io.IOExceptionAppend the text supplied by the Writer at the end of the File without writing a BOM, using a specified encoding.- Parameters:
- file- a File
- writer- the Writer supplying the text to append at the end of the File
- charset- the charset used
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
appendpublic static void append(java.nio.file.Path file, java.io.Writer writer, java.lang.String charset, boolean writeBom) throws java.io.IOExceptionAppend 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),writeBomistrue, and the file doesn't already exist, the requisite byte order mark is written to the file before the text is appended.- Parameters:
- file- a File
- writer- the Writer supplying the text to append at the end of the File
- charset- the charset used
- writeBom- whether to write the BOM
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
appendpublic static void append(java.nio.file.Path file, java.io.Reader reader, boolean writeBom) throws java.io.IOExceptionAppend 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),writeBomistrue, and the file doesn't already exist, the requisite byte order mark is written to the file before the text is appended.- Parameters:
- file- a File
- reader- the Reader supplying the text to append at the end of the File
- writeBom- whether to write the BOM
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
appendpublic static void append(java.nio.file.Path file, java.io.Reader reader, java.lang.String charset) throws java.io.IOExceptionAppend the text supplied by the Reader at the end of the File without writing a BOM, using a specified encoding.- Parameters:
- file- a File
- reader- the Reader supplying the text to append at the end of the File
- charset- the charset used
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
appendpublic static void append(java.nio.file.Path file, java.io.Reader reader, java.lang.String charset, boolean writeBom) throws java.io.IOExceptionAppend 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),writeBomistrue, and the file doesn't already exist, the requisite byte order mark is written to the file before the text is appended.- Parameters:
- file- a File
- reader- the Reader supplying the text to append at the end of the File
- charset- the charset used
- writeBom- whether to write the BOM
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
eachFilepublic static void eachFile(java.nio.file.Path self, FileType fileType, Closure closure) throws java.io.IOExceptionInvokes 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.- Parameters:
- self- a Path (that happens to be a folder/directory)
- fileType- if normal files or directories or both should be processed
- closure- the closure to invoke
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory
- java.io.IOException
- Since:
- 2.3.0
 
- 
eachFileInvokes the closure for each 'child' file in this 'parent' folder/directory. Both regular files and subfolders/subdirectories are processed.- Parameters:
- self- a Path (that happens to be a folder/directory)
- closure- a closure (the parameter is the Path for the 'child' file)
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory
- java.io.IOException
- Since:
- 2.3.0
- See Also:
- eachFile(Path, groovy.io.FileType, groovy.lang.Closure)
 
- 
eachDirInvokes the closure for each subdirectory in this directory, ignoring regular files.- Parameters:
- self- a Path (that happens to be a folder/directory)
- closure- a closure (the parameter is the Path for the subdirectory file)
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory
- java.io.IOException
- Since:
- 2.3.0
- See Also:
- eachFile(Path, groovy.io.FileType, groovy.lang.Closure)
 
- 
eachFileRecursepublic static void eachFileRecurse(java.nio.file.Path self, FileType fileType, Closure closure) throws java.io.IOExceptionProcesses each descendant file in this directory and any sub-directories. Processing consists of potentially callingclosurepassing 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.- Parameters:
- self- a Path (that happens to be a folder/directory)
- fileType- if normal files or directories or both should be processed
- closure- the closure to invoke on each file
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory
- java.io.IOException
- Since:
- 2.3.0
 
- 
traversepublic static void traverse(java.nio.file.Path self, java.util.Map<java.lang.String,java.lang.Object> options, Closure closure) throws java.io.IOExceptionProcesses each descendant file in this directory and any sub-directories. Processing consists of potentially callingclosurepassing 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 optionsMap according to the following keys:- type
- A FileTypeenum to determine if normal files or directories or both are processed
- preDir
- A Closurerun before each directory is processed and optionally returning aFileVisitResultvalue which can be used to control subsequent processing.
- preRoot
- A boolean indicating that the 'preDir' closure should be applied at the root level
- postDir
- A Closurerun after each directory is processed and optionally returning aFileVisitResultvalue which can be used to control subsequent processing.
- postRoot
- A boolean indicating that the 'postDir' closure should be applied at the root level
- visitRoot
- A boolean indicating that the given closure should be applied for the root dir
 (not applicable if the 'type' is set to FileType.FILES)
- maxDepth
- The maximum number of directory levels when recursing (default is -1 which means infinite, set to 0 for no recursion)
- filter
- A filter to perform on traversed files/directories (using the DefaultGroovyMethods.isCase(Object, Object)method). If set, only files/dirs which match are candidates for visiting.
- nameFilter
- A filter to perform on the name of traversed files/directories (using the DefaultGroovyMethods.isCase(Object, Object)method). If set, only files/dirs which match are candidates for visiting. (Must not be set if 'filter' is set)
- excludeFilter
- A filter to perform on traversed files/directories (using the DefaultGroovyMethods.isCase(Object, Object)method). If set, any candidates which match won't be visited.
- excludeNameFilter
- A filter to perform on the names of traversed files/directories (using the DefaultGroovyMethods.isCase(Object, Object)method). If set, any candidates which match won't be visited. (Must not be set if 'excludeFilter' is set)
- sort
- A Closurewhich 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++ }- Parameters:
- self- a Path (that happens to be a folder/directory)
- options- a Map of options to alter the traversal behavior
- closure- the Closure to invoke on each file/directory and optionally returning a- FileVisitResultvalue which can be used to control subsequent processing
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory or illegal filter combinations are supplied
- java.io.IOException
- Since:
- 2.3.0
- See Also:
- DefaultGroovyMethods.sort(java.lang.Iterable, groovy.lang.Closure),- FileVisitResult,- FileType
 
- 
traverseProcesses each descendant file in this directory and any sub-directories. Convenience method fortraverse(Path, java.util.Map, groovy.lang.Closure)when no options to alter the traversal behavior are required.- Parameters:
- self- a Path (that happens to be a folder/directory)
- closure- the Closure to invoke on each file/directory and optionally returning a- FileVisitResultvalue which can be used to control subsequent processing
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory
- java.io.IOException
- Since:
- 2.3.0
- See Also:
- traverse(Path, java.util.Map, groovy.lang.Closure)
 
- 
traversepublic static void traverse(java.nio.file.Path self, java.util.Map<java.lang.String,java.lang.Object> options) throws java.io.IOExceptionInvokes the closure specified with key 'visit' in the options Map for each descendant file in this directory tree. Convenience method fortraverse(Path, java.util.Map, groovy.lang.Closure)allowing the 'visit' closure to be included in the options Map rather than as a parameter.- Parameters:
- self- a Path (that happens to be a folder/directory)
- options- a Map of options to alter the traversal behavior
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory or illegal filter combinations are supplied
- java.io.IOException
- Since:
- 2.3.0
- See Also:
- traverse(Path, java.util.Map, groovy.lang.Closure)
 
- 
eachFileRecursepublic static void eachFileRecurse(java.nio.file.Path self, Closure closure) throws java.io.IOExceptionProcesses each descendant file in this directory and any sub-directories. Processing consists of callingclosurepassing it the current file (which may be a normal file or subdirectory) and then if a subdirectory was encountered, recursively processing the subdirectory.- Parameters:
- self- a Path (that happens to be a folder/directory)
- closure- a Closure
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory
- java.io.IOException
- Since:
- 2.3.0
- See Also:
- eachFileRecurse(Path, groovy.io.FileType, groovy.lang.Closure)
 
- 
eachDirRecursepublic static void eachDirRecurse(java.nio.file.Path self, Closure closure) throws java.io.IOExceptionRecursively processes each descendant subdirectory in this directory. Processing consists of callingclosurepassing it the current subdirectory and then recursively processing that subdirectory. Regular files are ignored during traversal.- Parameters:
- self- a Path (that happens to be a folder/directory)
- closure- a closure
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory
- java.io.IOException
- Since:
- 2.3.0
- See Also:
- eachFileRecurse(Path, groovy.io.FileType, groovy.lang.Closure)
 
- 
eachFileMatchpublic static void eachFileMatch(java.nio.file.Path self, FileType fileType, java.lang.Object nameFilter, Closure closure) throws java.io.IOExceptionInvokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling theDefaultGroovyMethods.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()}" }- Parameters:
- self- a Path (that happens to be a folder/directory)
- fileType- whether normal files or directories or both should be processed
- nameFilter- the filter to perform on the name of the file/directory (using the- DefaultGroovyMethods.isCase(Object, Object)method)
- closure- the closure to invoke
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory
- java.io.IOException
- Since:
- 2.3.0
 
- 
eachFileMatchpublic static void eachFileMatch(java.nio.file.Path self, java.lang.Object nameFilter, Closure closure) throws java.io.IOExceptionInvokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling theDefaultGroovyMethods.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.- Parameters:
- 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 invoke
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory
- java.io.IOException
- Since:
- 2.3.0
- See Also:
- eachFileMatch(Path, groovy.io.FileType, Object, groovy.lang.Closure)
 
- 
eachDirMatchpublic static void eachDirMatch(java.nio.file.Path self, java.lang.Object nameFilter, Closure closure) throws java.io.IOExceptionInvokes the closure for each subdirectory whose name (dir.name) matches the given nameFilter in the given directory - calling theDefaultGroovyMethods.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.- Parameters:
- 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 invoke
- Throws:
- java.io.FileNotFoundException- if the given directory does not exist
- java.lang.IllegalArgumentException- if the provided Path object does not represent a directory
- java.io.IOException
- Since:
- 2.3.0
- See Also:
- eachFileMatch(Path, groovy.io.FileType, Object, groovy.lang.Closure)
 
- 
deleteDirpublic static boolean deleteDir(java.nio.file.Path self)Deletes a directory with all contained files and subdirectories.The method returns - true, when deletion was successful
- true, when it is called for a non existing directory
- false, when it is called for a file which isn't a directory
- false, when directory couldn't be deleted
 - Parameters:
- self- a Path
- Returns:
- true if the file doesn't exist or deletion was successful
- Since:
- 2.3.0
 
- 
renameTopublic static boolean renameTo(java.nio.file.Path self, java.lang.String newPathName)Renames a file.- Parameters:
- self- a Path
- newPathName- The new pathname for the named file
- Returns:
- trueif and only if the renaming succeeded;- falseotherwise
- Since:
- 2.3.0
 
- 
renameTopublic static boolean renameTo(java.nio.file.Path self, java.net.URI newPathName)Renames a file.- Parameters:
- self- a Path
- newPathName- The new target path specified as a URI object
- Returns:
- trueif and only if the renaming succeeded;- falseotherwise
- Since:
- 2.3.0
 
- 
asWritablepublic static java.nio.file.Path asWritable(java.nio.file.Path self)Converts this Path to aWritable.- Parameters:
- self- a Path
- Returns:
- a Path which wraps the input file and which implements Writable
- Since:
- 2.3.0
 
- 
asTypepublic static <T> T asType(java.nio.file.Path path, java.lang.Class<T> c)Converts this Path to aWritableor delegates to defaultDefaultGroovyMethods.asType(Object, Class).- Parameters:
- path- a Path
- c- the desired class
- Returns:
- the converted object
- Since:
- 2.3.0
 
- 
asWritablepublic 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.- Parameters:
- self- a Path
- encoding- the encoding to be used when reading the file's contents
- Returns:
- Path which wraps the input file and which implements Writable
- Since:
- 2.3.0
 
- 
newReaderpublic static java.io.BufferedReader newReader(java.nio.file.Path self) throws java.io.IOExceptionCreate a buffered reader for this file.- Parameters:
- self- a Path
- Returns:
- a BufferedReader
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
newReaderpublic static java.io.BufferedReader newReader(java.nio.file.Path self, java.lang.String charset) throws java.io.IOExceptionCreate a buffered reader for this file, using the specified charset as the encoding.- Parameters:
- self- a Path
- charset- the charset for this Path
- Returns:
- a BufferedReader
- Throws:
- java.io.FileNotFoundException- if the Path was not found
- java.io.UnsupportedEncodingException- if the encoding specified is not supported
- java.io.IOException
- Since:
- 2.3.0
 
- 
withReaderpublic static <T> T withReader(java.nio.file.Path self, Closure<T> closure) throws java.io.IOExceptionCreate a new BufferedReader for this file and then passes it into the closure, ensuring the reader is closed after the closure returns.- Parameters:
- self- a file object
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
withReaderpublic static <T> T withReader(java.nio.file.Path self, java.lang.String charset, Closure<T> closure) throws java.io.IOExceptionCreate 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.- Parameters:
- self- a file object
- charset- the charset for this input stream
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
newOutputStreampublic static java.io.BufferedOutputStream newOutputStream(java.nio.file.Path self) throws java.io.IOExceptionCreate a buffered output stream for this file.- Parameters:
- self- a file object
- Returns:
- the created OutputStream
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
newDataOutputStreampublic static java.io.DataOutputStream newDataOutputStream(java.nio.file.Path self) throws java.io.IOExceptionCreates a new data output stream for this file.- Parameters:
- self- a file object
- Returns:
- the created DataOutputStream
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
withOutputStreampublic static <T> T withOutputStream(java.nio.file.Path self, Closure<T> closure) throws java.io.IOExceptionCreates a new OutputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.- Parameters:
- self- a Path
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.withStream(java.io.OutputStream, groovy.lang.Closure)
 
- 
withInputStreampublic static <T> T withInputStream(java.nio.file.Path self, Closure<T> closure) throws java.io.IOExceptionCreate a new InputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.- Parameters:
- self- a Path
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
 
- 
withDataOutputStreampublic static <T> T withDataOutputStream(java.nio.file.Path self, Closure<T> closure) throws java.io.IOExceptionCreate a new DataOutputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.- Parameters:
- self- a Path
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.withStream(java.io.OutputStream, groovy.lang.Closure)
 
- 
withDataInputStreampublic static <T> T withDataInputStream(java.nio.file.Path self, Closure<T> closure) throws java.io.IOExceptionCreate a new DataInputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.- Parameters:
- self- a Path
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
 
- 
newWriterpublic static java.io.BufferedWriter newWriter(java.nio.file.Path self) throws java.io.IOExceptionCreate a buffered writer for this file.- Parameters:
- self- a Path
- Returns:
- a BufferedWriter
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
newWriterpublic static java.io.BufferedWriter newWriter(java.nio.file.Path self, boolean append) throws java.io.IOExceptionCreates a buffered writer for this file, optionally appending to the existing file content.- Parameters:
- self- a Path
- append- true if data should be appended to the file
- Returns:
- a BufferedWriter
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
newWriterpublic static java.io.BufferedWriter newWriter(java.nio.file.Path self, java.lang.String charset, boolean append) throws java.io.IOExceptionHelper method to create a buffered writer for a file without writing a BOM.- Parameters:
- self- a Path
- charset- the name of the encoding used to write in this file
- append- true if in append mode
- Returns:
- a BufferedWriter
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
newWriterpublic static java.io.BufferedWriter newWriter(java.nio.file.Path self, java.lang.String charset, boolean append, boolean writeBom) throws java.io.IOExceptionHelper 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.- Parameters:
- self- a Path
- charset- the name of the encoding used to write in this file
- append- true if in append mode
- writeBom- whether to write a BOM
- Returns:
- a BufferedWriter
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
newWriterpublic static java.io.BufferedWriter newWriter(java.nio.file.Path self, java.lang.String charset) throws java.io.IOExceptionCreates a buffered writer for this file without writing a BOM, writing data using the given encoding.- Parameters:
- self- a Path
- charset- the name of the encoding used to write in this file
- Returns:
- a BufferedWriter
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
withWriterpublic static <T> T withWriter(java.nio.file.Path self, Closure<T> closure) throws java.io.IOExceptionCreates 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.- Parameters:
- self- a Path
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
withWriterpublic static <T> T withWriter(java.nio.file.Path self, java.lang.String charset, Closure<T> closure) throws java.io.IOExceptionCreates 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.- Parameters:
- self- a Path
- charset- the charset used
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
withWriterpublic static <T> T withWriter(java.nio.file.Path self, java.lang.String charset, boolean writeBom, Closure<T> closure) throws java.io.IOExceptionCreates 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),writeBomistrue, and the file doesn't already exist, the requisite byte order mark is written to the stream when the writer is created.- Parameters:
- self- a Path
- charset- the charset used
- writeBom- whether to write the BOM
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
withWriterAppendpublic static <T> T withWriterAppend(java.nio.file.Path self, java.lang.String charset, Closure<T> closure) throws java.io.IOExceptionCreate 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.- Parameters:
- self- a Path
- charset- the charset used
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
withWriterAppendpublic static <T> T withWriterAppend(java.nio.file.Path self, java.lang.String charset, boolean writeBom, Closure<T> closure) throws java.io.IOExceptionCreate 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),writeBomistrue, and the file doesn't already exist, the requisite byte order mark is written to the stream when the writer is created.- Parameters:
- self- a Path
- charset- the charset used
- writeBom- whether to write the BOM
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.5.0
 
- 
withWriterAppendpublic static <T> T withWriterAppend(java.nio.file.Path self, Closure<T> closure) throws java.io.IOExceptionCreate 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.- Parameters:
- self- a Path
- closure- a closure
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
newPrintWriterpublic static java.io.PrintWriter newPrintWriter(java.nio.file.Path self) throws java.io.IOExceptionCreate a new PrintWriter for this file.- Parameters:
- self- a Path
- Returns:
- the created PrintWriter
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
newPrintWriterpublic static java.io.PrintWriter newPrintWriter(java.nio.file.Path self, java.lang.String charset) throws java.io.IOExceptionCreate a new PrintWriter for this file, using specified charset.- Parameters:
- self- a Path
- charset- the charset
- Returns:
- a PrintWriter
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
withPrintWriterpublic static <T> T withPrintWriter(java.nio.file.Path self, Closure<T> closure) throws java.io.IOExceptionCreate 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.- Parameters:
- self- a Path
- closure- the closure to invoke with the PrintWriter
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
withPrintWriterpublic static <T> T withPrintWriter(java.nio.file.Path self, java.lang.String charset, Closure<T> closure) throws java.io.IOExceptionCreate 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.- Parameters:
- self- a Path
- charset- the charset
- closure- the closure to invoke with the PrintWriter
- Returns:
- the value returned by the closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
newInputStreampublic static java.io.BufferedInputStream newInputStream(java.nio.file.Path self) throws java.io.IOExceptionCreates a buffered input stream for this file.- Parameters:
- self- a Path
- Returns:
- a BufferedInputStream of the file
- Throws:
- java.io.FileNotFoundException- if the file is not found.
- java.io.IOException
- Since:
- 2.3.0
 
- 
newDataInputStreampublic static java.io.DataInputStream newDataInputStream(java.nio.file.Path self) throws java.io.IOExceptionCreate a data input stream for this file- Parameters:
- self- a Path
- Returns:
- a DataInputStream of the file
- Throws:
- java.io.FileNotFoundException- if the file is not found.
- java.io.IOException
- Since:
- 2.3.0
 
- 
eachByteTraverse through each byte of this Path- Parameters:
- self- a Path
- closure- a closure
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.eachByte(java.io.InputStream, groovy.lang.Closure)
 
- 
eachBytepublic static void eachByte(java.nio.file.Path self, int bufferLen, Closure closure) throws java.io.IOExceptionTraverse through the bytes of this Path, bufferLen bytes at a time.- Parameters:
- self- a Path
- bufferLen- the length of the buffer to use.
- closure- a 2 parameter closure which is passed the byte[] and a number of bytes successfully read.
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.eachByte(java.io.InputStream, int, groovy.lang.Closure)
 
- 
filterLinepublic static Writable filterLine(java.nio.file.Path self, Closure closure) throws java.io.IOExceptionFilters the lines of a Path and creates a Writable in return to stream the filtered lines.- Parameters:
- self- a Path
- closure- a closure which returns a boolean indicating to filter the line or not
- Returns:
- a Writable closure
- Throws:
- java.io.IOException- if- selfis not readable
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.filterLine(java.io.Reader, groovy.lang.Closure)
 
- 
filterLinepublic static Writable filterLine(java.nio.file.Path self, java.lang.String charset, Closure closure) throws java.io.IOExceptionFilters the lines of a Path and creates a Writable in return to stream the filtered lines.- Parameters:
- self- a Path
- charset- opens the file with a specified charset
- closure- a closure which returns a boolean indicating to filter the line or not
- Returns:
- a Writable closure
- Throws:
- java.io.IOException- if an IOException occurs
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.filterLine(java.io.Reader, groovy.lang.Closure)
 
- 
filterLinepublic static void filterLine(java.nio.file.Path self, java.io.Writer writer, Closure closure) throws java.io.IOExceptionFilter the lines from this Path, and write them to the given writer based on the given closure predicate.- Parameters:
- self- a Path
- writer- a writer destination to write filtered lines to
- closure- a closure which takes each line as a parameter and returns- trueif the line should be written to this writer.
- Throws:
- java.io.IOException- if- selfis not readable
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
 
- 
filterLinepublic static void filterLine(java.nio.file.Path self, java.io.Writer writer, java.lang.String charset, Closure closure) throws java.io.IOExceptionFilter the lines from this Path, and write them to the given writer based on the given closure predicate.- Parameters:
- self- a Path
- writer- a writer destination to write filtered lines to
- charset- opens the file with a specified charset
- closure- a closure which takes each line as a parameter and returns- trueif the line should be written to this writer.
- Throws:
- java.io.IOException- if an IO error occurs
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
 
- 
readBytespublic static byte[] readBytes(java.nio.file.Path self) throws java.io.IOExceptionReads the content of the file into a byte array.- Parameters:
- self- a Path
- Returns:
- a byte array with the contents of the file.
- Throws:
- java.io.IOException- if an IOException occurs.
- Since:
- 2.3.0
 
- 
withCloseable@Deprecated public static <T> T withCloseable(java.io.Closeable self, Closure<T> action) throws java.io.IOExceptionDeprecated.#deprecated use the variant in IOGroovyMethods- Throws:
- java.io.IOException
- Since:
- 2.3.0
- See Also:
- IOGroovyMethods.withCloseable(java.io.Closeable, groovy.lang.Closure)
 
 
-