Package org.codehaus.groovy.runtime
Class ResourceGroovyMethods
- java.lang.Object
-
- org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport
-
- org.codehaus.groovy.runtime.ResourceGroovyMethods
-
public class ResourceGroovyMethods 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 aeachLine(Closure c)
method forInputStream
.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 Summary
Constructors Constructor Description ResourceGroovyMethods()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
append(File file, byte[] bytes)
Append bytes to the end of a File.static void
append(File self, InputStream stream)
Append binary data to the file.static void
append(File file, Reader reader)
Append the text supplied by the Writer at the end of the File without writing a BOM.static void
append(File file, Reader reader, boolean writeBom)
Append the text supplied by the Reader at the end of the File, using a specified encoding.static void
append(File file, Reader reader, String charset)
Append the text supplied by the Reader at the end of the File without writing a BOM, using a specified encoding.static void
append(File file, Reader reader, String charset, boolean writeBom)
Append the text supplied by the Reader at the end of the File, using a specified encoding.static void
append(File file, Writer writer)
Append the text supplied by the Writer at the end of the File without writing a BOM.static void
append(File file, Writer writer, boolean writeBom)
Append the text supplied by the Writer at the end of the File.static void
append(File file, Writer writer, String charset)
Append the text supplied by the Writer at the end of the File without writing a BOM, using a specified encoding.static void
append(File file, Writer writer, String charset, boolean writeBom)
Append the text supplied by the Writer at the end of the File, using a specified encoding.static void
append(File file, Object text)
Append the text at the end of the File without writing a BOM.static void
append(File file, Object text, boolean writeBom)
Append the text at the end of the File.static void
append(File file, Object text, String charset)
Append the text at the end of the File without writing a BOM, using a specified encoding.static void
append(File file, Object text, String charset, boolean writeBom)
Append the text at the end of the File, using a specified encoding.static <T> T
asType(File f, Class<T> c)
Converts this File to aWritable
or delegates to defaultDefaultGroovyMethods.asType(java.lang.Object, java.lang.Class)
.static File
asWritable(File file)
Converts this File to aWritable
.static File
asWritable(File file, String encoding)
Allows a file to return a Writable implementation that can output itself to a Writer stream.static boolean
deleteDir(File self)
Deletes a directory with all contained files and subdirectories.static long
directorySize(File self)
Calculates directory size as total size of all its files, recursively.static void
eachByte(File self, int bufferLen, Closure closure)
Traverse through the bytes of this File, bufferLen bytes at a time.static void
eachByte(File self, Closure closure)
Traverse through each byte of this Filestatic void
eachByte(URL url, int bufferLen, Closure closure)
Reads the InputStream from this URL, passing a byte[] and a number of bytes to the given closure.static void
eachByte(URL url, Closure closure)
Reads the InputStream from this URL, passing each byte to the given closure.static void
eachDir(File self, Closure closure)
Invokes the closure for each subdirectory in this directory, ignoring regular files.static void
eachDirMatch(File self, Object nameFilter, Closure closure)
Invokes the closure for each subdirectory whose name (dir.name) matches the given nameFilter in the given directory - calling theDefaultGroovyMethods.isCase(java.lang.Object, java.lang.Object)
method to determine if a match occurs.static void
eachDirRecurse(File self, Closure closure)
Recursively processes each descendant subdirectory in this directory.static void
eachFile(File self, FileType fileType, Closure closure)
Invokes the closure for each 'child' file in this 'parent' folder/directory.static void
eachFile(File self, Closure closure)
Invokes the closure for each 'child' file in this 'parent' folder/directory.static void
eachFileMatch(File self, FileType fileType, Object nameFilter, Closure closure)
Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling theDefaultGroovyMethods.isCase(java.lang.Object, java.lang.Object)
method to determine if a match occurs.static void
eachFileMatch(File self, Object nameFilter, Closure closure)
Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling theDefaultGroovyMethods.isCase(java.lang.Object, java.lang.Object)
method to determine if a match occurs.static void
eachFileRecurse(File self, FileType fileType, Closure closure)
Processes each descendant file in this directory and any sub-directories.static void
eachFileRecurse(File self, Closure closure)
Processes each descendant file in this directory and any sub-directories.static <T> T
eachLine(File self, int firstLine, Closure<T> closure)
Iterates through this file line by line.static <T> T
eachLine(File self, Closure<T> closure)
Iterates through this file line by line.static <T> T
eachLine(File self, String charset, int firstLine, Closure<T> closure)
Iterates through this file line by line.static <T> T
eachLine(File self, String charset, Closure<T> closure)
Iterates through this file line by line.static <T> T
eachLine(URL url, int firstLine, Closure<T> closure)
Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure.static <T> T
eachLine(URL url, Closure<T> closure)
Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure.static <T> T
eachLine(URL url, String charset, int firstLine, Closure<T> closure)
Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure.static <T> T
eachLine(URL url, String charset, Closure<T> closure)
Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure.static void
eachObject(File self, Closure closure)
Iterates through the given file object by object.static Writable
filterLine(File self, Closure closure)
Filters the lines of a File and creates a Writable in return to stream the filtered lines.static void
filterLine(File self, Writer writer, Closure closure)
Filter the lines from this File, and write them to the given writer based on the given closure predicate.static void
filterLine(File self, Writer writer, String charset, Closure closure)
Filter the lines from this File, and write them to the given writer based on the given closure predicate.static Writable
filterLine(File self, String charset, Closure closure)
Filters the lines of a File and creates a Writable in return to stream the filtered lines.static Writable
filterLine(URL self, Closure predicate)
Filter lines from a URL using a closure predicate.static void
filterLine(URL self, Writer writer, Closure predicate)
Uses a closure to filter lines from this URL and pass them to the given writer.static void
filterLine(URL self, Writer writer, String charset, Closure predicate)
Uses a closure to filter lines from this URL and pass them to the given writer.static Writable
filterLine(URL self, String charset, Closure predicate)
Filter lines from a URL using a closure predicate.static byte[]
getBytes(File file)
Read the content of the File and returns it as a byte[].static byte[]
getBytes(URL url)
Read the content of this URL and returns it as a byte[].static byte[]
getBytes(URL url, Map parameters)
Read the content of this URL and returns it as a byte[].static String
getText(File file)
Read the content of the File and returns it as a String.static String
getText(File file, String charset)
Read the content of the File using the specified encoding and return it as a String.static String
getText(URL url)
Read the content of this URL and returns it as a String.static String
getText(URL url, String charset)
Read the data from this URL and return it as a String.static String
getText(URL url, Map parameters)
Read the content of this URL and returns it as a String.static String
getText(URL url, Map parameters, String charset)
Read the data from this URL and return it as a String.static File
leftShift(File file, byte[] bytes)
Write bytes to a File.static File
leftShift(File file, InputStream data)
Append binary data to the file.static File
leftShift(File file, Object text)
Write the text to the File.static DataInputStream
newDataInputStream(File file)
Create a data input stream for this filestatic DataOutputStream
newDataOutputStream(File file)
Creates a new data output stream for this file.static BufferedInputStream
newInputStream(File file)
Creates a buffered input stream for this file.static BufferedInputStream
newInputStream(URL url)
Creates a buffered input stream for this URL.static BufferedInputStream
newInputStream(URL url, Map parameters)
Creates a buffered input stream for this URL.static ObjectInputStream
newObjectInputStream(File file)
Create an object input stream for this file.static ObjectInputStream
newObjectInputStream(File file, ClassLoader classLoader)
Create an object input stream for this file using the given class loader.static ObjectOutputStream
newObjectOutputStream(File file)
Create an object output stream for this file.static BufferedOutputStream
newOutputStream(File file)
Create a buffered output stream for this file.static PrintWriter
newPrintWriter(File file)
Create a new PrintWriter for this file.static PrintWriter
newPrintWriter(File file, String charset)
Create a new PrintWriter for this file, using specified charset.static BufferedReader
newReader(File file)
Create a buffered reader for this file.static BufferedReader
newReader(File file, String charset)
Create a buffered reader for this file, using the specified charset as the encoding.static BufferedReader
newReader(URL url)
Creates a buffered reader for this URL.static BufferedReader
newReader(URL url, String charset)
Creates a buffered reader for this URL using the given encoding.static BufferedReader
newReader(URL url, Map parameters)
Creates a buffered reader for this URL.static BufferedReader
newReader(URL url, Map parameters, String charset)
Creates a buffered reader for this URL using the given encoding.static BufferedWriter
newWriter(File file)
Create a buffered writer for this file.static BufferedWriter
newWriter(File file, boolean append)
Creates a buffered writer for this file, optionally appending to the existing file content.static BufferedWriter
newWriter(File file, String charset)
Creates a buffered writer for this file, writing data without writing a BOM, using a specified encoding.static BufferedWriter
newWriter(File file, String charset, boolean append)
Helper method to create a buffered writer for a file without writing a BOM.static BufferedWriter
newWriter(File file, String charset, boolean append, boolean writeBom)
Helper method to create a buffered writer for a file.static byte[]
readBytes(File file)
Reads the content of the file into a byte array.static List<String>
readLines(File file)
Reads the file into a list of Strings, with one item for each line.static List<String>
readLines(File file, String charset)
Reads the file into a list of Strings, with one item for each line.static List<String>
readLines(URL self)
Reads the URL contents into a list, with one element for each line.static List<String>
readLines(URL self, String charset)
Reads the URL contents into a list, with one element for each line.static String
relativePath(File self, File to)
Relative path to file.static boolean
renameTo(File self, String newPathName)
Renames the file.static void
setBytes(File file, byte[] bytes)
Write the bytes from the byte array to the File.static void
setText(File file, String text)
Synonym for write(text) allowing file.text = 'foo'.static void
setText(File file, String text, String charset)
Synonym for write(text, charset) allowing:static long
size(File self)
Provide the standard Groovysize()
method forFile
.static <T> T
splitEachLine(File self, String regex, Closure<T> closure)
Iterates through this file line by line, splitting each line using the given regex separator.static <T> T
splitEachLine(File self, String regex, String charset, Closure<T> closure)
Iterates through this file line by line, splitting each line using the given regex separator.static <T> T
splitEachLine(File self, Pattern pattern, Closure<T> closure)
Iterates through this file line by line, splitting each line using the given separator Pattern.static <T> T
splitEachLine(File self, Pattern pattern, String charset, Closure<T> closure)
Iterates through this file line by line, splitting each line using the given regex separator Pattern.static <T> T
splitEachLine(URL self, String regex, Closure<T> closure)
Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator.static <T> T
splitEachLine(URL self, String regex, String charset, Closure<T> closure)
Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator.static <T> T
splitEachLine(URL self, Pattern pattern, Closure<T> closure)
Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator Pattern.static <T> T
splitEachLine(URL self, Pattern pattern, String charset, Closure<T> closure)
Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator Pattern.static URI
toURI(CharSequence self)
Transforms a CharSequence representing a URI into a URI object.static URI
toURI(String self)
Transforms a String representing a URI into a URI object.static URL
toURL(CharSequence self)
Transforms a CharSequence representing a URL into a URL object.static URL
toURL(String self)
Transforms a String representing a URL into a URL object.static void
traverse(File self, Closure closure)
Processes each descendant file in this directory and any sub-directories.static void
traverse(File self, Map<String,?> options)
Invokes the closure specified with key 'visit' in the options Map for each descendant file in this directory tree.static void
traverse(File self, Map<String,?> options, Closure closure)
Processes each descendant file in this directory and any sub-directories.static <T> T
withDataInputStream(File file, Closure<T> closure)
Create a new DataInputStream for this file and passes it into the closure.static <T> T
withDataOutputStream(File file, Closure<T> closure)
Create a new DataOutputStream for this file and passes it into the closure.static Object
withInputStream(File file, Closure closure)
Create a new InputStream for this file and passes it into the closure.static <T> T
withInputStream(URL url, Closure<T> closure)
Creates a new InputStream for this URL and passes it into the closure.static <T> T
withObjectInputStream(File file, Closure<T> closure)
Create a new ObjectInputStream for this file and pass it to the closure.static <T> T
withObjectInputStream(File file, ClassLoader classLoader, Closure<T> closure)
Create a new ObjectInputStream for this file associated with the given class loader and pass it to the closure.static <T> T
withObjectOutputStream(File file, Closure<T> closure)
Create a new ObjectOutputStream for this file and then pass it to the closure.static Object
withOutputStream(File file, Closure closure)
Creates a new OutputStream for this file and passes it into the closure.static <T> T
withPrintWriter(File file, Closure<T> closure)
Create a new PrintWriter for this file which is then passed it into the given closure.static <T> T
withPrintWriter(File file, String charset, Closure<T> closure)
Create a new PrintWriter with a specified charset for this file.static <T> T
withReader(File file, Closure<T> closure)
Create a new BufferedReader for this file and then passes it into the closure, ensuring the reader is closed after the closure returns.static <T> T
withReader(File file, String charset, Closure<T> closure)
Create a new BufferedReader for this file using the specified charset and then passes it into the closure, ensuring the reader is closed after the closure returns.static <T> T
withReader(URL url, Closure<T> closure)
Helper method to create a new BufferedReader for a URL and then passes it to the closure.static <T> T
withReader(URL url, String charset, Closure<T> closure)
Helper method to create a new Reader for a URL and then passes it to the closure.static <T> T
withWriter(File file, Closure<T> closure)
Creates a new BufferedWriter for this file, passes it to the closure, and ensures the stream is flushed and closed after the closure returns.static <T> T
withWriter(File file, String charset, Closure<T> closure)
Creates a new BufferedWriter for this file, passes it to the closure, and ensures the stream is flushed and closed after the closure returns.static <T> T
withWriterAppend(File file, Closure<T> closure)
Create a new BufferedWriter for this file in append mode.static <T> T
withWriterAppend(File file, String charset, Closure<T> closure)
Create a new BufferedWriter which will append to this file.static void
write(File file, String text)
Write the text to the File without writing a BOM.static void
write(File file, String text, boolean writeBom)
Write the text to the File.static void
write(File file, String text, String charset)
Write the text to the File without writing a BOM, using the specified encoding.static void
write(File file, String text, String charset, boolean writeBom)
Write the text to the File, using the specified encoding.-
Methods inherited from class org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport
cloneSimilarCollection, cloneSimilarMap, closeQuietly, closeWithWarning, createSimilarArray, createSimilarCollection, createSimilarCollection, createSimilarCollection, createSimilarList, createSimilarMap, createSimilarOrDefaultCollection, createSimilarQueue, createSimilarSet, normaliseIndex, sameType, subListBorders, subListBorders
-
-
-
-
Method Detail
-
size
public static long size(File self)
Provide the standard Groovysize()
method forFile
.- Parameters:
self
- a file object- Returns:
- the file's size (length)
- Since:
- 1.5.0
-
directorySize
public static long directorySize(File self) throws IOException, IllegalArgumentException
Calculates directory size as total size of all its files, recursively.- Parameters:
self
- a file object- Returns:
- directory size (length)
- Throws:
IOException
- if File object specified does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 2.1
-
newObjectOutputStream
public static ObjectOutputStream newObjectOutputStream(File file) throws IOException
Create an object output stream for this file.- Parameters:
file
- a file- Returns:
- an object output stream
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.0
-
withObjectOutputStream
public static <T> T withObjectOutputStream(File file, Closure<T> closure) throws IOException
Create a new ObjectOutputStream for this file and then pass it to the closure. This method ensures the stream is closed after the closure returns.- Parameters:
file
- a Fileclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.0
- See Also:
IOGroovyMethods.withStream(java.io.OutputStream, groovy.lang.Closure)
-
newObjectInputStream
public static ObjectInputStream newObjectInputStream(File file) throws IOException
Create an object input stream for this file.- Parameters:
file
- a file- Returns:
- an object input stream
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.0
-
newObjectInputStream
public static ObjectInputStream newObjectInputStream(File file, ClassLoader classLoader) throws IOException
Create an object input stream for this file using the given class loader.- Parameters:
file
- a fileclassLoader
- the class loader to use when loading the class- Returns:
- an object input stream
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.0
-
eachObject
public static void eachObject(File self, Closure closure) throws IOException, ClassNotFoundException
Iterates through the given file object by object.- Parameters:
self
- a Fileclosure
- a closure- Throws:
IOException
- if an IOException occurs.ClassNotFoundException
- if the class is not found.- Since:
- 1.0
- See Also:
IOGroovyMethods.eachObject(java.io.ObjectInputStream, groovy.lang.Closure)
-
withObjectInputStream
public static <T> T withObjectInputStream(File file, Closure<T> closure) throws IOException
Create a new ObjectInputStream for this file and pass it to the closure. This method ensures the stream is closed after the closure returns.- Parameters:
file
- a Fileclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
- See Also:
IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
-
withObjectInputStream
public static <T> T withObjectInputStream(File file, ClassLoader classLoader, Closure<T> closure) throws IOException
Create 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:
file
- a FileclassLoader
- the class loader to use when loading the classclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
- See Also:
IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
-
eachLine
public static <T> T eachLine(File self, Closure<T> closure) throws IOException
Iterates through this file line by line. Each line is passed to the given 1 or 2 arg closure. The file is read using a reader which is closed before this method returns.- Parameters:
self
- a Fileclosure
- 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:
IOException
- if an IOException occurs.- Since:
- 1.5.5
- See Also:
eachLine(java.io.File, int, groovy.lang.Closure)
-
eachLine
public static <T> T eachLine(File self, String charset, Closure<T> closure) throws IOException
Iterates through this file line by line. Each line is passed to the given 1 or 2 arg closure. The file is read using a reader which is closed before this method returns.- Parameters:
self
- a Filecharset
- opens the file with a specified charsetclosure
- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
eachLine(java.io.File, java.lang.String, int, groovy.lang.Closure)
-
eachLine
public static <T> T eachLine(File self, int firstLine, Closure<T> closure) throws IOException
Iterates through this file line by line. Each line is passed to the given 1 or 2 arg closure. The file is read using a reader which is closed before this method returns.- Parameters:
self
- a FilefirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure (arg 1 is line, optional arg 2 is line number)- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.7
- See Also:
IOGroovyMethods.eachLine(java.io.Reader, int, groovy.lang.Closure)
-
eachLine
public static <T> T eachLine(File self, String charset, int firstLine, Closure<T> closure) throws IOException
Iterates through this file line by line. Each line is passed to the given 1 or 2 arg closure. The file is read using a reader which is closed before this method returns.- Parameters:
self
- a Filecharset
- opens the file with a specified charsetfirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure (arg 1 is line, optional arg 2 is line number)- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
IOGroovyMethods.eachLine(java.io.Reader, int, groovy.lang.Closure)
-
eachLine
public static <T> T eachLine(URL url, Closure<T> closure) throws IOException
Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.- Parameters:
url
- a URL to open and readclosure
- a closure to apply on each line (arg 1 is line, optional arg 2 is line number starting at line 1)- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.6
- See Also:
eachLine(java.net.URL, int, groovy.lang.Closure)
-
eachLine
public static <T> T eachLine(URL url, int firstLine, Closure<T> closure) throws IOException
Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.- Parameters:
url
- a URL to open and readfirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure to apply on each line (arg 1 is line, optional arg 2 is line number)- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.7
- See Also:
IOGroovyMethods.eachLine(java.io.InputStream, int, groovy.lang.Closure)
-
eachLine
public static <T> T eachLine(URL url, String charset, Closure<T> closure) throws IOException
Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.- Parameters:
url
- a URL to open and readcharset
- opens the stream with a specified charsetclosure
- a closure to apply on each line (arg 1 is line, optional arg 2 is line number starting at line 1)- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.6
- See Also:
eachLine(java.net.URL, java.lang.String, int, groovy.lang.Closure)
-
eachLine
public static <T> T eachLine(URL url, String charset, int firstLine, Closure<T> closure) throws IOException
Iterates through the lines read from the URL's associated input stream passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.- Parameters:
url
- a URL to open and readcharset
- opens the stream with a specified charsetfirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure to apply on each line (arg 1 is line, optional arg 2 is line number)- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.7
- See Also:
IOGroovyMethods.eachLine(java.io.Reader, int, groovy.lang.Closure)
-
splitEachLine
public static <T> T splitEachLine(File self, String regex, Closure<T> closure) throws IOException
Iterates 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 Fileregex
- the delimiting regular expressionclosure
- a closure- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.PatternSyntaxException
- if the regular expression's syntax is invalid- Since:
- 1.5.5
- See Also:
IOGroovyMethods.splitEachLine(java.io.Reader, java.lang.String, groovy.lang.Closure)
-
splitEachLine
public static <T> T splitEachLine(File self, Pattern pattern, Closure<T> closure) throws IOException
Iterates 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 Filepattern
- the regular expression Pattern for the delimiterclosure
- a closure- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
IOGroovyMethods.splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
-
splitEachLine
public static <T> T splitEachLine(File self, String regex, String charset, Closure<T> closure) throws IOException
Iterates 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 Fileregex
- the delimiting regular expressioncharset
- opens the file with a specified charsetclosure
- a closure- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.PatternSyntaxException
- if the regular expression's syntax is invalid- Since:
- 1.6.8
- See Also:
IOGroovyMethods.splitEachLine(java.io.Reader, java.lang.String, groovy.lang.Closure)
-
splitEachLine
public static <T> T splitEachLine(File self, Pattern pattern, String charset, Closure<T> closure) throws IOException
Iterates 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 Filepattern
- the regular expression Pattern for the delimitercharset
- opens the file with a specified charsetclosure
- a closure- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
IOGroovyMethods.splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
-
splitEachLine
public static <T> T splitEachLine(URL self, String regex, Closure<T> closure) throws IOException
Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the URL are closed.- Parameters:
self
- a URL to open and readregex
- the delimiting regular expressionclosure
- a closure- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.PatternSyntaxException
- if the regular expression's syntax is invalid- Since:
- 1.6.8
- See Also:
IOGroovyMethods.splitEachLine(java.io.Reader, java.lang.String, groovy.lang.Closure)
-
splitEachLine
public static <T> T splitEachLine(URL self, Pattern pattern, Closure<T> closure) throws IOException
Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator Pattern. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the URL are closed.- Parameters:
self
- a URL to open and readpattern
- the regular expression Pattern for the delimiterclosure
- a closure- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
IOGroovyMethods.splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
-
splitEachLine
public static <T> T splitEachLine(URL self, String regex, String charset, Closure<T> closure) throws IOException
Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the URL are closed.- Parameters:
self
- a URL to open and readregex
- the delimiting regular expressioncharset
- opens the file with a specified charsetclosure
- a closure- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.PatternSyntaxException
- if the regular expression's syntax is invalid- Since:
- 1.6.8
- See Also:
IOGroovyMethods.splitEachLine(java.io.Reader, java.lang.String, groovy.lang.Closure)
-
splitEachLine
public static <T> T splitEachLine(URL self, Pattern pattern, String charset, Closure<T> closure) throws IOException
Iterates through the input stream associated with this URL line by line, splitting each line using the given regex separator Pattern. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. Finally the resources used for processing the URL are closed.- Parameters:
self
- a URL to open and readpattern
- the regular expression Pattern for the delimitercharset
- opens the file with a specified charsetclosure
- a closure- Returns:
- the last value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
IOGroovyMethods.splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
-
readLines
public static List<String> readLines(File file) throws IOException
Reads the file into a list of Strings, with one item for each line.- Parameters:
file
- a File- Returns:
- a List of lines
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
- See Also:
IOGroovyMethods.readLines(java.io.Reader)
-
readLines
public static List<String> readLines(File file, String charset) throws IOException
Reads the file into a list of Strings, with one item for each line.- Parameters:
file
- a Filecharset
- opens the file with a specified charset- Returns:
- a List of lines
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
IOGroovyMethods.readLines(java.io.Reader)
-
readLines
public static List<String> readLines(URL self) throws IOException
Reads the URL contents into a list, with one element for each line.- Parameters:
self
- a URL- Returns:
- a List of lines
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
IOGroovyMethods.readLines(java.io.Reader)
-
readLines
public static List<String> readLines(URL self, String charset) throws IOException
Reads the URL contents into a list, with one element for each line.- Parameters:
self
- a URLcharset
- opens the URL with a specified charset- Returns:
- a List of lines
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
IOGroovyMethods.readLines(java.io.Reader)
-
getText
public static String getText(File file, String charset) throws IOException
Read the content of the File using the specified encoding and return it as a String.- Parameters:
file
- the file whose content we want to readcharset
- the charset used to read the content of the file- Returns:
- a String containing the content of the file
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
getText
public static String getText(File file) throws IOException
Read the content of the File and returns it as a String.- Parameters:
file
- the file whose content we want to read- Returns:
- a String containing the content of the file
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
getText
public static String getText(URL url) throws IOException
Read the content of this URL and returns it as a String.- Parameters:
url
- URL to read content from- Returns:
- the text from that URL
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
getText
public static String getText(URL url, Map parameters) throws IOException
Read the content of this URL and returns it as a String. The default connection parameters can be modified by adding keys to the parameters map:- connectTimeout : the connection timeout
- readTimeout : the read timeout
- useCaches : set the use cache property for the URL connection
- allowUserInteraction : set the user interaction flag for the URL connection
- requestProperties : a map of properties to be passed to the URL connection
- Parameters:
url
- URL to read content fromparameters
- connection parameters- Returns:
- the text from that URL
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.8.1
-
getText
public static String getText(URL url, String charset) throws IOException
Read the data from this URL and return it as a String. The connection stream is closed before this method returns.- Parameters:
url
- URL to read content fromcharset
- opens the stream with a specified charset- Returns:
- the text from that URL
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
- See Also:
URLConnection.getInputStream()
-
getText
public static String getText(URL url, Map parameters, String charset) throws IOException
Read the data from this URL and return it as a String. The connection stream is closed before this method returns.- Parameters:
url
- URL to read content fromparameters
- connection parameterscharset
- opens the stream with a specified charset- Returns:
- the text from that URL
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.8.1
- See Also:
URLConnection.getInputStream()
-
getBytes
public static byte[] getBytes(File file) throws IOException
Read the content of the File and returns it as a byte[].- Parameters:
file
- the file whose content we want to read- Returns:
- a String containing the content of the file
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.7.1
-
getBytes
public static byte[] getBytes(URL url) throws IOException
Read the content of this URL and returns it as a byte[].- Parameters:
url
- URL to read content from- Returns:
- the byte[] from that URL
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.7.1
-
getBytes
public static byte[] getBytes(URL url, Map parameters) throws IOException
Read the content of this URL and returns it as a byte[]. The default connection parameters can be modified by adding keys to the parameters map:- connectTimeout : the connection timeout
- readTimeout : the read timeout
- useCaches : set the use cache property for the URL connection
- allowUserInteraction : set the user interaction flag for the URL connection
- requestProperties : a map of properties to be passed to the URL connection
- Parameters:
url
- URL to read content fromparameters
- connection parameters- Returns:
- the byte[] from that URL
- Throws:
IOException
- if an IOException occurs.- Since:
- 2.4.4
-
setBytes
public static void setBytes(File file, byte[] bytes) throws IOException
Write the bytes from the byte array to the File.- Parameters:
file
- the file to write tobytes
- the byte[] to write to the file- Throws:
IOException
- if an IOException occurs.- Since:
- 1.7.1
-
write
public static void write(File file, String text) throws IOException
Write the text to the File without writing a BOM.- Parameters:
file
- a Filetext
- the text to write to the File- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
write
public static void write(File file, String text, boolean writeBom) throws IOException
Write the text to the File. If the default charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) andwriteBom
istrue
, the requisite byte order mark is written to the file before the text.- Parameters:
file
- a Filetext
- the text to write to the FilewriteBom
- whether to write a BOM- Throws:
IOException
- if an IOException occurs.- Since:
- 2.5.0
-
setText
public static void setText(File file, String text) throws IOException
Synonym for write(text) allowing file.text = 'foo'.- Parameters:
file
- a Filetext
- the text to write to the File- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.1
- See Also:
write(java.io.File, java.lang.String)
-
setText
public static void setText(File file, String text, String charset) throws IOException
Synonym 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:
file
- A Filecharset
- The charset used when writing to the filetext
- The text to write to the File- Throws:
IOException
- if an IOException occurs.- Since:
- 1.7.3
- See Also:
write(java.io.File, java.lang.String, java.lang.String)
-
leftShift
public static File leftShift(File file, Object text) throws IOException
Write the text to the File.- Parameters:
file
- a Filetext
- the text to write to the File- Returns:
- the original file
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
leftShift
public static File leftShift(File file, byte[] bytes) throws IOException
Write bytes to a File.- Parameters:
file
- a Filebytes
- the byte array to append to the end of the File- Returns:
- the original file
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.0
-
leftShift
public static File leftShift(File file, InputStream data) throws IOException
Append binary data to the file. Seeappend(java.io.File, java.io.InputStream)
- Parameters:
file
- a Filedata
- an InputStream of data to write to the file- Returns:
- the file
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.0
-
write
public static void write(File file, String text, String charset) throws IOException
Write the text to the File without writing a BOM, using the specified encoding.- Parameters:
file
- a Filetext
- the text to write to the Filecharset
- the charset used- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
write
public static void write(File file, String text, String charset, boolean writeBom) throws IOException
Write the text to the File, using the specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) andwriteBom
istrue
, the requisite byte order mark is written to the file before the text.- Parameters:
file
- a Filetext
- the text to write to the Filecharset
- the charset usedwriteBom
- whether to write a BOM- Throws:
IOException
- if an IOException occurs.- Since:
- 2.5.0
-
append
public static void append(File file, Object text) throws IOException
Append the text at the end of the File without writing a BOM.- Parameters:
file
- a Filetext
- the text to append at the end of the File- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
append
public static void append(File file, Object text, boolean writeBom) throws IOException
Append the text at the end of the File. If the default charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) andwriteBom
istrue
, the requisite byte order mark is written to the file before the text.- Parameters:
file
- a Filetext
- the text to append at the end of the FilewriteBom
- whether to write a BOM- Throws:
IOException
- if an IOException occurs.- Since:
- 2.5.0
-
append
public static void append(File file, Reader reader) throws IOException
Append the text supplied by the Writer at the end of the File without writing a BOM.- Parameters:
file
- a Filereader
- the Reader supplying the text to append at the end of the File- Throws:
IOException
- if an IOException occurs.- Since:
- 2.3
-
append
public static void append(File file, Writer writer) throws IOException
Append the text supplied by the Writer at the end of the File without writing a BOM.- Parameters:
file
- a Filewriter
- the Writer supplying the text to append at the end of the File- Throws:
IOException
- if an IOException occurs.- Since:
- 2.3
-
append
public static void append(File file, Writer writer, boolean writeBom) throws IOException
Append the text supplied by the Writer at the end of the File. If the default charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) andwriteBom
istrue
, the requisite byte order mark is written to the file before the text.- Parameters:
file
- a Filewriter
- the Writer supplying the text to append at the end of the FilewriteBom
- whether to write a BOM- Throws:
IOException
- if an IOException occurs.- Since:
- 2.5.0
-
append
public static void append(File file, byte[] bytes) throws IOException
Append bytes to the end of a File. It will not be interpreted as text.- Parameters:
file
- a Filebytes
- the byte array to append to the end of the File- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.1
-
append
public static void append(File self, InputStream stream) throws IOException
Append binary data to the file. It will not be interpreted as text.- Parameters:
self
- a Filestream
- stream to read data from.- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.0
-
append
public static void append(File file, Object text, String charset) throws IOException
Append the text at the end of the File without writing a BOM, using a specified encoding.- Parameters:
file
- a Filetext
- the text to append at the end of the Filecharset
- the charset used- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
append
public static void append(File file, Object text, String charset, boolean writeBom) throws IOException
Append the text at the end of the File, using a specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),writeBom
istrue
, 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 Filetext
- the text to append at the end of the Filecharset
- the charset usedwriteBom
- whether to write a BOM- Throws:
IOException
- if an IOException occurs.- Since:
- 2.5.0
-
append
public static void append(File file, Writer writer, String charset) throws IOException
Append the text supplied by the Writer at the end of the File without writing a BOM, using a specified encoding.- Parameters:
file
- a Filewriter
- the Writer supplying the text to append at the end of the Filecharset
- the charset used- Throws:
IOException
- if an IOException occurs.- Since:
- 2.3
-
append
public static void append(File file, Writer writer, String charset, boolean writeBom) throws IOException
Append the text supplied by the Writer at the end of the File, using a specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),writeBom
istrue
, 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 Filewriter
- the Writer supplying the text to append at the end of the Filecharset
- the charset usedwriteBom
- whether to write a BOM- Throws:
IOException
- if an IOException occurs.- Since:
- 2.5.0
-
append
public static void append(File file, Reader reader, boolean writeBom) throws IOException
Append the text supplied by the Reader at the end of the File, using a specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),writeBom
istrue
, 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 Filereader
- the Reader supplying the text to append at the end of the FilewriteBom
- whether to write a BOM- Throws:
IOException
- if an IOException occurs.- Since:
- 2.5.0
-
append
public static void append(File file, Reader reader, String charset) throws IOException
Append the text supplied by the Reader at the end of the File without writing a BOM, using a specified encoding.- Parameters:
file
- a Filereader
- the Reader supplying the text to append at the end of the Filecharset
- the charset used- Throws:
IOException
- if an IOException occurs.- Since:
- 2.3
-
append
public static void append(File file, Reader reader, String charset, boolean writeBom) throws IOException
Append the text supplied by the Reader at the end of the File, using a specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),writeBom
istrue
, 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 Filereader
- the Reader supplying the text to append at the end of the Filecharset
- the charset usedwriteBom
- whether to write a BOM- Throws:
IOException
- if an IOException occurs.- Since:
- 2.5.0
-
eachFile
public static void eachFile(File self, FileType fileType, Closure closure) throws FileNotFoundException, IllegalArgumentException
Invokes 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 File (that happens to be a folder/directory)fileType
- if normal files or directories or both should be processedclosure
- the closure to invoke- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 1.7.1
-
eachFile
public static void eachFile(File self, Closure closure) throws FileNotFoundException, IllegalArgumentException
Invokes the closure for each 'child' file in this 'parent' folder/directory. Both regular files and subfolders/subdirectories are processed.- Parameters:
self
- a File (that happens to be a folder/directory)closure
- a closure (the parameter passed is the 'child' file)- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 1.5.0
- See Also:
File.listFiles()
,eachFile(java.io.File, groovy.io.FileType, groovy.lang.Closure)
-
eachDir
public static void eachDir(File self, Closure closure) throws FileNotFoundException, IllegalArgumentException
Invokes the closure for each subdirectory in this directory, ignoring regular files.- Parameters:
self
- a File (that happens to be a folder/directory)closure
- a closure (the parameter passed is the subdirectory file)- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 1.0
- See Also:
File.listFiles()
,eachFile(java.io.File, groovy.io.FileType, groovy.lang.Closure)
-
eachFileRecurse
public static void eachFileRecurse(File self, FileType fileType, Closure closure) throws FileNotFoundException, IllegalArgumentException
Processes each descendant file in this directory and any sub-directories. Processing consists of potentially callingclosure
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.- Parameters:
self
- a File (that happens to be a folder/directory)fileType
- if normal files or directories or both should be processedclosure
- the closure to invoke on each file- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 1.7.1
-
traverse
public static void traverse(File self, Map<String,?> options, Closure closure) throws FileNotFoundException, IllegalArgumentException
Processes each descendant file in this directory and any sub-directories. Processing consists of potentially callingclosure
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:- type
- A
FileType
enum to determine if normal files or directories or both are processed - preDir
- A
Closure
run before each directory is processed and optionally returning aFileVisitResult
value 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
Closure
run after each directory is processed and optionally returning aFileVisitResult
value which can be used to control subsequent processing. Particularly useful when strict depth-first traversal is required. - 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(java.lang.Object, java.lang.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(java.lang.Object, java.lang.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(java.lang.Object, java.lang.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(java.lang.Object, java.lang.Object)
method). If set, any candidates which match won't be visited. (Must not be set if 'excludeFilter' is set) - sort
- A
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++ }- Parameters:
self
- a File (that happens to be a folder/directory)options
- a Map of options to alter the traversal behaviorclosure
- the Closure to invoke on each file/directory and optionally returning aFileVisitResult
value which can be used to control subsequent processing- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory or illegal filter combinations are supplied- Since:
- 1.7.1
- See Also:
DefaultGroovyMethods.sort(java.util.Collection, groovy.lang.Closure)
,FileVisitResult
,FileType
-
traverse
public static void traverse(File self, Closure closure) throws FileNotFoundException, IllegalArgumentException
Processes each descendant file in this directory and any sub-directories. Convenience method fortraverse(java.io.File, java.util.Map, groovy.lang.Closure)
when no options to alter the traversal behavior are required.- Parameters:
self
- a File (that happens to be a folder/directory)closure
- the Closure to invoke on each file/directory and optionally returning aFileVisitResult
value which can be used to control subsequent processing- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 1.7.1
- See Also:
traverse(java.io.File, java.util.Map, groovy.lang.Closure)
-
traverse
public static void traverse(File self, Map<String,?> options) throws FileNotFoundException, IllegalArgumentException
Invokes the closure specified with key 'visit' in the options Map for each descendant file in this directory tree. Convenience method fortraverse(java.io.File, java.util.Map, groovy.lang.Closure)
allowing the 'visit' closure to be included in the options Map rather than as a parameter.- Parameters:
self
- a File (that happens to be a folder/directory)options
- a Map of options to alter the traversal behavior- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory or illegal filter combinations are supplied- Since:
- 1.7.1
- See Also:
traverse(java.io.File, java.util.Map, groovy.lang.Closure)
-
eachFileRecurse
public static void eachFileRecurse(File self, Closure closure) throws FileNotFoundException, IllegalArgumentException
Processes each descendant file in this directory and any sub-directories. Processing consists of callingclosure
passing 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 File (that happens to be a folder/directory)closure
- a Closure- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 1.0
- See Also:
eachFileRecurse(java.io.File, groovy.io.FileType, groovy.lang.Closure)
-
eachDirRecurse
public static void eachDirRecurse(File self, Closure closure) throws FileNotFoundException, IllegalArgumentException
Recursively processes each descendant subdirectory in this directory. Processing consists of callingclosure
passing it the current subdirectory and then recursively processing that subdirectory. Regular files are ignored during traversal.- Parameters:
self
- a File (that happens to be a folder/directory)closure
- a closure- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 1.5.0
- See Also:
eachFileRecurse(java.io.File, groovy.io.FileType, groovy.lang.Closure)
-
eachFileMatch
public static void eachFileMatch(File self, FileType fileType, Object nameFilter, Closure closure) throws FileNotFoundException, IllegalArgumentException
Invokes the closure for each file whose name (file.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. Both regular files and subdirectories may be candidates for matching depending on the value of fileType.// collect names of files in baseDir matching supplied regex pattern import static groovy.io.FileType.* def names = [] baseDir.eachFileMatch FILES, ~/foo\d\.txt/, { names
<<
it.name } assert names == ['foo1.txt', 'foo2.txt'] // remove all *.bak files in baseDir baseDir.eachFileMatch FILES, ~/.*\.bak/, { File bak->
bak.delete() } // print out files > 4K in size from baseDir baseDir.eachFileMatch FILES, { new File(baseDir, it).size()>
4096 }, { println "$it.name ${it.size()}" }- Parameters:
self
- a File (that happens to be a folder/directory)fileType
- whether normal files or directories or both should be processednameFilter
- the filter to perform on the name of the file/directory (using theDefaultGroovyMethods.isCase(java.lang.Object, java.lang.Object)
method)closure
- the closure to invoke- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 1.7.1
-
eachFileMatch
public static void eachFileMatch(File self, Object nameFilter, Closure closure) throws FileNotFoundException, IllegalArgumentException
Invokes the closure for each file whose name (file.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. Both regular files and subdirectories are matched.- Parameters:
self
- a File (that happens to be a folder/directory)nameFilter
- the nameFilter to perform on the name of the file (using theDefaultGroovyMethods.isCase(java.lang.Object, java.lang.Object)
method)closure
- the closure to invoke- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 1.5.0
- See Also:
eachFileMatch(java.io.File, groovy.io.FileType, java.lang.Object, groovy.lang.Closure)
-
eachDirMatch
public static void eachDirMatch(File self, Object nameFilter, Closure closure) throws FileNotFoundException, IllegalArgumentException
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. 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 File (that happens to be a folder/directory)nameFilter
- the nameFilter to perform on the name of the directory (using theDefaultGroovyMethods.isCase(java.lang.Object, java.lang.Object)
method)closure
- the closure to invoke- Throws:
FileNotFoundException
- if the given directory does not existIllegalArgumentException
- if the provided File object does not represent a directory- Since:
- 1.5.0
- See Also:
eachFileMatch(java.io.File, groovy.io.FileType, java.lang.Object, groovy.lang.Closure)
-
deleteDir
public static boolean deleteDir(File 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 File- Returns:
- true if the file doesn't exist or deletion was successful
- Since:
- 1.6.0
-
renameTo
public static boolean renameTo(File self, String newPathName)
Renames the file. It's a shortcut forFile.renameTo(File)
- Parameters:
self
- a FilenewPathName
- The new pathname for the named file- Returns:
true
if and only if the renaming succeeded;false
otherwise- Since:
- 1.7.4
-
relativePath
public static String relativePath(File self, File to) throws IOException
Relative path to file.- Parameters:
self
- theFile
to calculate the path fromto
- theFile
to calculate the path to- Returns:
- the relative path between the files
- Throws:
IOException
-
asWritable
public static File asWritable(File file)
Converts this File to aWritable
.- Parameters:
file
- a File- Returns:
- a File which wraps the input file and which implements Writable
- Since:
- 1.0
-
asType
public static <T> T asType(File f, Class<T> c)
Converts this File to aWritable
or delegates to defaultDefaultGroovyMethods.asType(java.lang.Object, java.lang.Class)
.- Parameters:
f
- a Filec
- the desired class- Returns:
- the converted object
- Since:
- 1.0
-
asWritable
public static File asWritable(File file, String encoding)
Allows a file to return a Writable implementation that can output itself to a Writer stream.- Parameters:
file
- a Fileencoding
- the encoding to be used when reading the file's contents- Returns:
- File which wraps the input file and which implements Writable
- Since:
- 1.0
-
newReader
public static BufferedReader newReader(File file) throws IOException
Create a buffered reader for this file.- Parameters:
file
- a File- Returns:
- a BufferedReader
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
newReader
public static BufferedReader newReader(File file, String charset) throws FileNotFoundException, UnsupportedEncodingException
Create a buffered reader for this file, using the specified charset as the encoding.- Parameters:
file
- a Filecharset
- the charset for this File- Returns:
- a BufferedReader
- Throws:
FileNotFoundException
- if the File was not foundUnsupportedEncodingException
- if the encoding specified is not supported- Since:
- 1.0
-
withReader
public static <T> T withReader(File file, Closure<T> closure) throws IOException
Create a new BufferedReader for this file and then passes it into the closure, ensuring the reader is closed after the closure returns.- Parameters:
file
- a file objectclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
-
withReader
public static <T> T withReader(File file, String charset, Closure<T> closure) throws IOException
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.- Parameters:
file
- a file objectcharset
- the charset for this input streamclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.0
-
newOutputStream
public static BufferedOutputStream newOutputStream(File file) throws IOException
Create a buffered output stream for this file.- Parameters:
file
- a file object- Returns:
- the created OutputStream
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
newDataOutputStream
public static DataOutputStream newDataOutputStream(File file) throws IOException
Creates a new data output stream for this file.- Parameters:
file
- a file object- Returns:
- the created DataOutputStream
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.0
-
withOutputStream
public static Object withOutputStream(File file, Closure closure) throws IOException
Creates a new OutputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.- Parameters:
file
- a Fileclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
- See Also:
IOGroovyMethods.withStream(java.io.OutputStream, groovy.lang.Closure)
-
withInputStream
public static Object withInputStream(File file, Closure closure) throws IOException
Create a new InputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.- Parameters:
file
- a Fileclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
- See Also:
IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
-
withInputStream
public static <T> T withInputStream(URL url, Closure<T> closure) throws IOException
Creates a new InputStream for this URL and passes it into the closure. This method ensures the stream is closed after the closure returns.- Parameters:
url
- a URLclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
- See Also:
IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
-
withDataOutputStream
public static <T> T withDataOutputStream(File file, Closure<T> closure) throws IOException
Create a new DataOutputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.- Parameters:
file
- a Fileclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
- See Also:
IOGroovyMethods.withStream(java.io.OutputStream, groovy.lang.Closure)
-
withDataInputStream
public static <T> T withDataInputStream(File file, Closure<T> closure) throws IOException
Create a new DataInputStream for this file and passes it into the closure. This method ensures the stream is closed after the closure returns.- Parameters:
file
- a Fileclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
- See Also:
IOGroovyMethods.withStream(java.io.InputStream, groovy.lang.Closure)
-
newWriter
public static BufferedWriter newWriter(File file) throws IOException
Create a buffered writer for this file.- Parameters:
file
- a File- Returns:
- a BufferedWriter
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
newWriter
public static BufferedWriter newWriter(File file, boolean append) throws IOException
Creates a buffered writer for this file, optionally appending to the existing file content.- Parameters:
file
- a Fileappend
- true if data should be appended to the file- Returns:
- a BufferedWriter
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
newWriter
public static BufferedWriter newWriter(File file, String charset, boolean append) throws IOException
Helper method to create a buffered writer for a file without writing a BOM.- Parameters:
file
- a Filecharset
- the name of the encoding used to write in this fileappend
- true if in append mode- Returns:
- a BufferedWriter
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
newWriter
public static BufferedWriter newWriter(File file, String charset, boolean append, boolean writeBom) throws IOException
Helper 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:
file
- a Filecharset
- the name of the encoding used to write in this fileappend
- true if in append modewriteBom
- whether to write a BOM- Returns:
- a BufferedWriter
- Throws:
IOException
- if an IOException occurs.- Since:
- 2.5.0
-
newWriter
public static BufferedWriter newWriter(File file, String charset) throws IOException
Creates a buffered writer for this file, writing data without writing a BOM, using a specified encoding.- Parameters:
file
- a Filecharset
- the name of the encoding used to write in this file- Returns:
- a BufferedWriter
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
withWriter
public static <T> T withWriter(File file, Closure<T> closure) throws IOException
Creates a new BufferedWriter for this file, passes it to the closure, and ensures the stream is flushed and closed after the closure returns.- Parameters:
file
- a Fileclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
-
withWriter
public static <T> T withWriter(File file, String charset, Closure<T> closure) throws IOException
Creates a new BufferedWriter for this file, passes it to the closure, and ensures the stream is flushed and closed after the closure returns. The writer will use the given charset encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the requisite byte order mark is written to the stream when the writer is created.- Parameters:
file
- a Filecharset
- the charset usedclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
-
withWriterAppend
public static <T> T withWriterAppend(File file, String charset, Closure<T> closure) throws IOException
Create a new BufferedWriter which will append to this file. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the requisite byte order mark is written to the stream when the writer is created. The writer is passed to the closure and will be closed before this method returns.- Parameters:
file
- a Filecharset
- the charset usedclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
-
withWriterAppend
public static <T> T withWriterAppend(File file, Closure<T> closure) throws IOException
Create a new BufferedWriter for this file in append mode. The writer is passed to the closure and is closed after the closure returns.- Parameters:
file
- a Fileclosure
- a closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
-
newPrintWriter
public static PrintWriter newPrintWriter(File file) throws IOException
Create a new PrintWriter for this file.- Parameters:
file
- a File- Returns:
- the created PrintWriter
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
newPrintWriter
public static PrintWriter newPrintWriter(File file, String charset) throws IOException
Create a new PrintWriter for this file, using specified charset. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the requisite byte order mark is written to the stream before the writer is returned.- Parameters:
file
- a Filecharset
- the charset- Returns:
- a PrintWriter
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
withPrintWriter
public static <T> T withPrintWriter(File file, Closure<T> closure) throws IOException
Create 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:
file
- a Fileclosure
- the closure to invoke with the PrintWriter- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
-
withPrintWriter
public static <T> T withPrintWriter(File file, String charset, Closure<T> closure) throws IOException
Create a new PrintWriter with a specified charset for this file. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the requisite byte order mark is written to the stream when the writer is created. The writer is passed to the closure, and will be closed before this method returns.- Parameters:
file
- a Filecharset
- the charsetclosure
- the closure to invoke with the PrintWriter- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
-
withReader
public static <T> T withReader(URL url, Closure<T> closure) throws IOException
Helper method to create a new BufferedReader for a URL and then passes it to the closure. The reader is closed after the closure returns.- Parameters:
url
- a URLclosure
- the closure to invoke with the reader- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
-
withReader
public static <T> T withReader(URL url, String charset, Closure<T> closure) throws IOException
Helper method to create a new Reader for a URL and then passes it to the closure. The reader is closed after the closure returns.- Parameters:
url
- a URLcharset
- the charset usedclosure
- the closure to invoke with the reader- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.6
-
newInputStream
public static BufferedInputStream newInputStream(File file) throws FileNotFoundException
Creates a buffered input stream for this file.- Parameters:
file
- a File- Returns:
- a BufferedInputStream of the file
- Throws:
FileNotFoundException
- if the file is not found.- Since:
- 1.0
-
newInputStream
public static BufferedInputStream newInputStream(URL url) throws MalformedURLException, IOException
Creates a buffered input stream for this URL.- Parameters:
url
- a URL- Returns:
- a BufferedInputStream for the URL
- Throws:
MalformedURLException
- is thrown if the URL is not well formedIOException
- if an I/O error occurs while creating the input stream- Since:
- 1.5.2
-
newInputStream
public static BufferedInputStream newInputStream(URL url, Map parameters) throws MalformedURLException, IOException
Creates a buffered input stream for this URL. The default connection parameters can be modified by adding keys to the parameters map:- connectTimeout : the connection timeout
- readTimeout : the read timeout
- useCaches : set the use cache property for the URL connection
- allowUserInteraction : set the user interaction flag for the URL connection
- requestProperties : a map of properties to be passed to the URL connection
- Parameters:
url
- a URLparameters
- connection parameters- Returns:
- a BufferedInputStream for the URL
- Throws:
MalformedURLException
- is thrown if the URL is not well formedIOException
- if an I/O error occurs while creating the input stream- Since:
- 1.8.1
-
newReader
public static BufferedReader newReader(URL url) throws MalformedURLException, IOException
Creates a buffered reader for this URL.- Parameters:
url
- a URL- Returns:
- a BufferedReader for the URL
- Throws:
MalformedURLException
- is thrown if the URL is not well formedIOException
- if an I/O error occurs while creating the input stream- Since:
- 1.5.5
-
newReader
public static BufferedReader newReader(URL url, Map parameters) throws MalformedURLException, IOException
Creates a buffered reader for this URL. The default connection parameters can be modified by adding keys to the parameters map:- connectTimeout : the connection timeout
- readTimeout : the read timeout
- useCaches : set the use cache property for the URL connection
- allowUserInteraction : set the user interaction flag for the URL connection
- requestProperties : a map of properties to be passed to the URL connection
- Parameters:
url
- a URLparameters
- connection parameters- Returns:
- a BufferedReader for the URL
- Throws:
MalformedURLException
- is thrown if the URL is not well formedIOException
- if an I/O error occurs while creating the input stream- Since:
- 1.8.1
-
newReader
public static BufferedReader newReader(URL url, String charset) throws MalformedURLException, IOException
Creates a buffered reader for this URL using the given encoding.- Parameters:
url
- a URLcharset
- opens the stream with a specified charset- Returns:
- a BufferedReader for the URL
- Throws:
MalformedURLException
- is thrown if the URL is not well formedIOException
- if an I/O error occurs while creating the input stream- Since:
- 1.5.5
-
newReader
public static BufferedReader newReader(URL url, Map parameters, String charset) throws MalformedURLException, IOException
Creates a buffered reader for this URL using the given encoding.- Parameters:
url
- a URLparameters
- connection parameterscharset
- opens the stream with a specified charset- Returns:
- a BufferedReader for the URL
- Throws:
MalformedURLException
- is thrown if the URL is not well formedIOException
- if an I/O error occurs while creating the input stream- Since:
- 1.8.1
-
newDataInputStream
public static DataInputStream newDataInputStream(File file) throws FileNotFoundException
Create a data input stream for this file- Parameters:
file
- a File- Returns:
- a DataInputStream of the file
- Throws:
FileNotFoundException
- if the file is not found.- Since:
- 1.5.0
-
eachByte
public static void eachByte(File self, Closure closure) throws IOException
Traverse through each byte of this File- Parameters:
self
- a Fileclosure
- a closure- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
- See Also:
IOGroovyMethods.eachByte(java.io.InputStream, groovy.lang.Closure)
-
eachByte
public static void eachByte(File self, int bufferLen, Closure closure) throws IOException
Traverse through the bytes of this File, bufferLen bytes at a time.- Parameters:
self
- a FilebufferLen
- the length of the buffer to use.closure
- a 2 parameter closure which is passed the byte[] and a number of bytes successfully read.- Throws:
IOException
- if an IOException occurs.- Since:
- 1.7.4
- See Also:
IOGroovyMethods.eachByte(java.io.InputStream, int, groovy.lang.Closure)
-
eachByte
public static void eachByte(URL url, Closure closure) throws IOException
Reads the InputStream from this URL, passing each byte to the given closure. The URL stream will be closed before this method returns.- Parameters:
url
- url to iterate overclosure
- closure to apply to each byte- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
- See Also:
IOGroovyMethods.eachByte(java.io.InputStream, groovy.lang.Closure)
-
eachByte
public static void eachByte(URL url, int bufferLen, Closure closure) throws IOException
Reads the InputStream from this URL, passing a byte[] and a number of bytes to the given closure. The URL stream will be closed before this method returns.- Parameters:
url
- url to iterate overbufferLen
- the length of the buffer to use.closure
- a 2 parameter closure which is passed the byte[] and a number of bytes successfully read.- Throws:
IOException
- if an IOException occurs.- Since:
- 1.8
- See Also:
IOGroovyMethods.eachByte(java.io.InputStream, int, groovy.lang.Closure)
-
filterLine
public static Writable filterLine(File self, Closure closure) throws IOException
Filters the lines of a File and creates a Writable in return to stream the filtered lines.- Parameters:
self
- a Fileclosure
- a closure which returns a boolean indicating to filter the line or not- Returns:
- a Writable closure
- Throws:
IOException
- ifself
is not readable- Since:
- 1.0
- See Also:
IOGroovyMethods.filterLine(java.io.Reader, groovy.lang.Closure)
-
filterLine
public static Writable filterLine(File self, String charset, Closure closure) throws IOException
Filters the lines of a File and creates a Writable in return to stream the filtered lines.- Parameters:
self
- a Filecharset
- opens the file with a specified charsetclosure
- a closure which returns a boolean indicating to filter the line or not- Returns:
- a Writable closure
- Throws:
IOException
- if an IOException occurs- Since:
- 1.6.8
- See Also:
IOGroovyMethods.filterLine(java.io.Reader, groovy.lang.Closure)
-
filterLine
public static void filterLine(File self, Writer writer, Closure closure) throws IOException
Filter the lines from this File, and write them to the given writer based on the given closure predicate.- Parameters:
self
- a Filewriter
- a writer destination to write filtered lines toclosure
- a closure which takes each line as a parameter and returnstrue
if the line should be written to this writer.- Throws:
IOException
- ifself
is not readable- Since:
- 1.0
- See Also:
IOGroovyMethods.filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
-
filterLine
public static void filterLine(File self, Writer writer, String charset, Closure closure) throws IOException
Filter the lines from this File, and write them to the given writer based on the given closure predicate.- Parameters:
self
- a Filewriter
- a writer destination to write filtered lines tocharset
- opens the file with a specified charsetclosure
- a closure which takes each line as a parameter and returnstrue
if the line should be written to this writer.- Throws:
IOException
- if an IO error occurs- Since:
- 1.6.8
- See Also:
IOGroovyMethods.filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
-
filterLine
public static Writable filterLine(URL self, Closure predicate) throws IOException
Filter lines from a URL using a closure predicate. The closure will be passed each line as a String, and it should returntrue
if the line should be passed to the writer.- Parameters:
self
- a URLpredicate
- a closure which returns boolean and takes a line- Returns:
- a writable which writes out the filtered lines
- Throws:
IOException
- if an IO exception occurs- Since:
- 1.6.8
- See Also:
IOGroovyMethods.filterLine(java.io.Reader, groovy.lang.Closure)
-
filterLine
public static Writable filterLine(URL self, String charset, Closure predicate) throws IOException
Filter lines from a URL using a closure predicate. The closure will be passed each line as a String, and it should returntrue
if the line should be passed to the writer.- Parameters:
self
- the URLcharset
- opens the URL with a specified charsetpredicate
- a closure which returns boolean and takes a line- Returns:
- a writable which writes out the filtered lines
- Throws:
IOException
- if an IO exception occurs- Since:
- 1.6.8
- See Also:
IOGroovyMethods.filterLine(java.io.Reader, groovy.lang.Closure)
-
filterLine
public static void filterLine(URL self, Writer writer, Closure predicate) throws IOException
Uses a closure to filter lines from this URL and pass them to the given writer. The closure will be passed each line as a String, and it should returntrue
if the line should be passed to the writer.- Parameters:
self
- the URLwriter
- a writer to write output topredicate
- a closure which returns true if a line should be accepted- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
IOGroovyMethods.filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
-
filterLine
public static void filterLine(URL self, Writer writer, String charset, Closure predicate) throws IOException
Uses a closure to filter lines from this URL and pass them to the given writer. The closure will be passed each line as a String, and it should returntrue
if the line should be passed to the writer.- Parameters:
self
- the URLwriter
- a writer to write output tocharset
- opens the URL with a specified charsetpredicate
- a closure which returns true if a line should be accepted- Throws:
IOException
- if an IOException occurs.- Since:
- 1.6.8
- See Also:
IOGroovyMethods.filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
-
readBytes
public static byte[] readBytes(File file) throws IOException
Reads the content of the file into a byte array.- Parameters:
file
- a File- Returns:
- a byte array with the contents of the file.
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
toURI
public static URI toURI(CharSequence self) throws URISyntaxException
Transforms a CharSequence representing a URI into a URI object.- Parameters:
self
- the CharSequence representing a URI- Returns:
- a URI
- Throws:
URISyntaxException
- is thrown if the URI is not well formed.- Since:
- 1.8.2
-
toURI
public static URI toURI(String self) throws URISyntaxException
Transforms a String representing a URI into a URI object.- Parameters:
self
- the String representing a URI- Returns:
- a URI
- Throws:
URISyntaxException
- is thrown if the URI is not well formed.- Since:
- 1.0
-
toURL
public static URL toURL(CharSequence self) throws MalformedURLException
Transforms a CharSequence representing a URL into a URL object.- Parameters:
self
- the CharSequence representing a URL- Returns:
- a URL
- Throws:
MalformedURLException
- is thrown if the URL is not well formed.- Since:
- 1.8.2
-
toURL
public static URL toURL(String self) throws MalformedURLException
Transforms a String representing a URL into a URL object.- Parameters:
self
- the String representing a URL- Returns:
- a URL
- Throws:
MalformedURLException
- is thrown if the URL is not well formed.- Since:
- 1.0
-
-