Package: java.io

## Class File

- - ### Methods Summary

    Methods
    Return type | Name and parameters
    --- | ---
    `void` | `append(byte[] bytes)` Append bytes to the end of a File.
    `void` | `append(InputStream stream)` Append binary data to the file.
    `void` | `append(Reader reader)` Append the text supplied by the Writer at the end of the File without writing a BOM.
    `void` | `append(Reader reader, boolean writeBom)` Append the text supplied by the Reader at the end of the File, using a specified encoding.
    `void` | `append(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.
    `void` | `append(Reader reader, String charset, boolean writeBom)` Append the text supplied by the Reader at the end of the File, using a specified encoding.
    `void` | `append(Writer writer)` Append the text supplied by the Writer at the end of the File without writing a BOM.
    `void` | `append(Writer writer, boolean writeBom)` Append the text supplied by the Writer at the end of the File.
    `void` | `append(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.
    `void` | `append(Writer writer, String charset, boolean writeBom)` Append the text supplied by the Writer at the end of the File, using a specified encoding.
    `void` | `append(Object text)` Append the text at the end of the File without writing a BOM.
    `void` | `append(Object text, boolean writeBom)` Append the text at the end of the File.
    `void` | `append(Object text, String charset)` Append the text at the end of the File without writing a BOM, using a specified encoding.
    `void` | `append(Object text, String charset, boolean writeBom)` Append the text at the end of the File, using a specified encoding.
    `Object` | `asType(Class c)` Converts this File to a Writable or delegates to default DefaultGroovyMethods#asType(java.lang.Object, java.lang.Class).
    `File` | `asWritable()` Converts this File to a Writable.
    `File` | `asWritable(String encoding)` Allows a file to return a Writable implementation that can output itself to a Writer stream.
    `static File` | `createTempDir()`
    `static File` | `createTempDir(String prefix, String suffix)`
    `boolean` | `deleteDir()` Deletes a directory with all contained files and subdirectories.
    `long` | `directorySize()` Calculates directory size as total size of all its files, recursively.
    `void` | `eachByte(Closure closure)` Traverse through each byte of this File
    `void` | `eachByte(int bufferLen, Closure closure)` Traverse through the bytes of this File, bufferLen bytes at a time.
    `void` | `eachDir(Closure closure)` Invokes the closure for each subdirectory in this directory, ignoring regular files.
    `void` | `eachDirMatch(Object nameFilter, Closure closure)` Invokes the closure for each subdirectory whose name (dir.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object) method to determine if a match occurs.
    `void` | `eachDirRecurse(Closure closure)` Recursively processes each descendant subdirectory in this directory.
    `void` | `eachFile(FileType fileType, Closure closure)` Invokes the closure for each 'child' file in this 'parent' folder/directory.
    `void` | `eachFile(Closure closure)` Invokes the closure for each 'child' file in this 'parent' folder/directory.
    `void` | `eachFileMatch(FileType fileType, Object nameFilter, Closure closure)` Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object) method to determine if a match occurs.
    `void` | `eachFileMatch(Object nameFilter, Closure closure)` Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object) method to determine if a match occurs.
    `void` | `eachFileRecurse(FileType fileType, Closure closure)` Processes each descendant file in this directory and any sub-directories.
    `void` | `eachFileRecurse(Closure closure)` Processes each descendant file in this directory and any sub-directories.
    `Object` | `eachLine(Closure closure)` Iterates through this file line by line.
    `Object` | `eachLine(int firstLine, Closure closure)` Iterates through this file line by line.
    `Object` | `eachLine(String charset, Closure closure)` Iterates through this file line by line.
    `Object` | `eachLine(String charset, int firstLine, Closure closure)` Iterates through this file line by line.
    `void` | `eachObject(Closure closure)` Iterates through the given file object by object.
    `Writable` | `filterLine(Closure closure)` Filters the lines of a File and creates a Writable in return to stream the filtered lines.
    `void` | `filterLine(Writer writer, Closure closure)` Filter the lines from this File, and write them to the given writer based on the given closure predicate.
    `void` | `filterLine(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.
    `Writable` | `filterLine(String charset, Closure closure)` Filters the lines of a File and creates a Writable in return to stream the filtered lines.
    `byte[]` | `getBytes()` Read the content of the File and returns it as a byte[].
    `String` | `getText()` Read the content of the File and returns it as a String.
    `String` | `getText(String charset)` Read the content of the File using the specified encoding and return it as a String.
    `File` | `leftShift(byte[] bytes)` Write bytes to a File.
    `File` | `leftShift(InputStream data)` Append binary data to the file.
    `File` | `leftShift(Object text)` Write the text to the File.
    `DataInputStream` | `newDataInputStream()` Create a data input stream for this file
    `DataOutputStream` | `newDataOutputStream()` Creates a new data output stream for this file.
    `BufferedInputStream` | `newInputStream()` Creates a buffered input stream for this file.
    `ObjectInputStream` | `newObjectInputStream()` Create an object input stream for this file.
    `ObjectInputStream` | `newObjectInputStream(ClassLoader classLoader)` Create an object input stream for this file using the given class loader.
    `ObjectOutputStream` | `newObjectOutputStream()` Create an object output stream for this file.
    `BufferedOutputStream` | `newOutputStream()` Create a buffered output stream for this file.
    `PrintWriter` | `newPrintWriter()` Create a new PrintWriter for this file.
    `PrintWriter` | `newPrintWriter(String charset)` Create a new PrintWriter for this file, using specified charset.
    `BufferedReader` | `newReader()` Create a buffered reader for this file.
    `BufferedReader` | `newReader(String charset)` Create a buffered reader for this file, using the specified charset as the encoding.
    `BufferedWriter` | `newWriter()` Create a buffered writer for this file.
    `BufferedWriter` | `newWriter(boolean append)` Creates a buffered writer for this file, optionally appending to the existing file content.
    `BufferedWriter` | `newWriter(String charset)` Creates a buffered writer for this file, writing data without writing a BOM, using a specified encoding.
    `BufferedWriter` | `newWriter(String charset, boolean append)` Helper method to create a buffered writer for a file without writing a BOM.
    `BufferedWriter` | `newWriter(String charset, boolean append, boolean writeBom)` Helper method to create a buffered writer for a file.
    `byte[]` | `readBytes()` Reads the content of the file into a byte array.
    `List` | `readLines()` Reads the file into a list of Strings, with one item for each line.
    `List` | `readLines(String charset)` Reads the file into a list of Strings, with one item for each line.
    `String` | `relativePath(File to)` Relative path to file.
    `boolean` | `renameTo(String newPathName)` Renames the file.
    `void` | `setBytes(byte[] bytes)` Write the bytes from the byte array to the File.
    `void` | `setText(String text)` Synonym for write(text) allowing file.text = 'foo'.
    `void` | `setText(String text, String charset)` Synonym for write(text, charset) allowing:
        myFile.setText('some text', charset)
    or with some help from `ExpandoMetaClass`, you could do something like:
        myFile.metaClass.setText = { String s -> delegate.setText(s, 'UTF-8') }
        myfile.text = 'some text'
    `long` | `size()` Provide the standard Groovy `size()` method for `File`.
    `Object` | `splitEachLine(String regex, Closure closure)` Iterates through this file line by line, splitting each line using the given regex separator.
    `Object` | `splitEachLine(String regex, String charset, Closure closure)` Iterates through this file line by line, splitting each line using the given regex separator.
    `Object` | `splitEachLine(Pattern pattern, Closure closure)` Iterates through this file line by line, splitting each line using the given separator Pattern.
    `Object` | `splitEachLine(Pattern pattern, String charset, Closure closure)` Iterates through this file line by line, splitting each line using the given regex separator Pattern.
    `void` | `traverse(Closure closure)` Processes each descendant file in this directory and any sub-directories.
    `void` | `traverse(Map options)` Invokes the closure specified with key 'visit' in the options Map for each descendant file in this directory tree.
    `void` | `traverse(Map options, Closure closure)` Processes each descendant file in this directory and any sub-directories.
    `Object` | `withDataInputStream(Closure closure)` Create a new DataInputStream for this file and passes it into the closure.
    `Object` | `withDataOutputStream(Closure closure)` Create a new DataOutputStream for this file and passes it into the closure.
    `Object` | `withInputStream(Closure closure)` Create a new InputStream for this file and passes it into the closure.
    `Object` | `withObjectInputStream(Closure closure)` Create a new ObjectInputStream for this file and pass it to the closure.
    `Object` | `withObjectInputStream(ClassLoader classLoader, Closure closure)` Create a new ObjectInputStream for this file associated with the given class loader and pass it to the closure.
    `Object` | `withObjectOutputStream(Closure closure)` Create a new ObjectOutputStream for this file and then pass it to the closure.
    `Object` | `withOutputStream(Closure closure)` Creates a new OutputStream for this file and passes it into the closure.
    `Object` | `withPrintWriter(Closure closure)` Create a new PrintWriter for this file which is then passed it into the given closure.
    `Object` | `withPrintWriter(String charset, Closure closure)` Create a new PrintWriter with a specified charset for this file.
    `Object` | `withReader(Closure closure)` Create a new BufferedReader for this file and then passes it into the closure, ensuring the reader is closed after the closure returns.
    `Object` | `withReader(String charset, Closure 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.
    `Object` | `withWriter(Closure 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.
    `Object` | `withWriter(String charset, Closure 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.
    `Object` | `withWriterAppend(Closure closure)` Create a new BufferedWriter for this file in append mode.
    `Object` | `withWriterAppend(String charset, Closure closure)` Create a new BufferedWriter which will append to this file.
    `void` | `write(String text)` Write the text to the File without writing a BOM.
    `void` | `write(String text, boolean writeBom)` Write the text to the File.
    `void` | `write(String text, String charset)` Write the text to the File without writing a BOM, using the specified encoding.
    `void` | `write(String text, String charset, boolean writeBom)` Write the text to the File, using the specified encoding.

  - #### Methods inherited from class java.lang.Object

    `addShutdownHook`, `any`, `any`, `asBoolean`, `asType`, `collect`, `collect`, `collect`, `contains`, `count`, `dump`, `each`, `eachWithIndex`, `equals`, `every`, `every`, `find`, `find`, `findAll`, `findAll`, `findIndexOf`, `findIndexOf`, `findIndexValues`, `findIndexValues`, `findLastIndexOf`, `findLastIndexOf`, `findResult`, `findResult`, `flatten`, `getAt`, `getMetaClass`, `getMetaPropertyValues`, `getProperties`, `grep`, `grep`, `groupBy`, `groupBy`, `hasProperty`, `identity`, `inject`, `inject`, `inspect`, `invokeMethod`, `is`, `isCase`, `iterator`, `join`, `metaClass`, `print`, `print`, `printf`, `printf`, `println`, `println`, `println`, `putAt`, `respondsTo`, `respondsTo`, `setMetaClass`, `size`, `split`, `sprintf`, `sprintf`, `sum`, `sum`, `tap`, `toArrayString`, `toSpreadMap`, `toString`, `use`, `use`, `use`, `with`, `with`, `withTraits`

  - #### Methods inherited from interface java.lang.Comparable

    `numberAwareCompareTo`

- - ### Methods Detail

    - #### public void append(byte[] bytes)

      Append bytes to the end of a File. It will not be interpreted as text.

      Parameters:
        `bytes` - the byte array to append to the end of the File
      Since:
        1.5.1

    - #### public void append(InputStream stream)

      Append binary data to the file. It will not be interpreted as text.

      Parameters:
        `stream` - stream to read data from.
      Since:
        1.5.0

    - #### public void append(Reader reader)

      Append the text supplied by the Writer at the end of the File without writing a BOM.

      Parameters:
        `reader` - the Reader supplying the text to append at the end of the File
      Since:
        2.3

    - #### public void append(Reader reader, boolean writeBom)

      Append the text supplied by the Reader at the end of the File, using a specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), `writeBom` is `true`, and the file doesn't already exist, the requisite byte order mark is written to the file before the text is appended.

      Parameters:
        `reader` - the Reader supplying the text to append at the end of the File
        `writeBom` - whether to write a BOM
      Since:
        2.5.0

    - #### public void append(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.

      Parameters:
        `reader` - the Reader supplying the text to append at the end of the File
        `charset` - the charset used
      Since:
        2.3

    - #### public void append(Reader reader, String charset, boolean writeBom)

      Append the text supplied by the Reader at the end of the File, using a specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), `writeBom` is `true`, and the file doesn't already exist, the requisite byte order mark is written to the file before the text is appended.

      Parameters:
        `reader` - the Reader supplying the text to append at the end of the File
        `charset` - the charset used
        `writeBom` - whether to write a BOM
      Since:
        2.5.0

    - #### public void append(Writer writer)

      Append the text supplied by the Writer at the end of the File without writing a BOM.

      Parameters:
        `writer` - the Writer supplying the text to append at the end of the File
      Since:
        2.3

    - #### public void append(Writer writer, boolean writeBom)

      Append the text supplied by the Writer at the end of the File. If the default charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) and `writeBom` is `true`, the requisite byte order mark is written to the file before the text.

      Parameters:
        `writer` - the Writer supplying the text to append at the end of the File
        `writeBom` - whether to write a BOM
      Since:
        2.5.0

    - #### public void append(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.

      Parameters:
        `writer` - the Writer supplying the text to append at the end of the File
        `charset` - the charset used
      Since:
        2.3

    - #### public void append(Writer writer, String charset, boolean writeBom)

      Append the text supplied by the Writer at the end of the File, using a specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), `writeBom` is `true`, and the file doesn't already exist, the requisite byte order mark is written to the file before the text is appended.

      Parameters:
        `writer` - the Writer supplying the text to append at the end of the File
        `charset` - the charset used
        `writeBom` - whether to write a BOM
      Since:
        2.5.0

    - #### public void append(Object text)

      Append the text at the end of the File without writing a BOM.

      Parameters:
        `text` - the text to append at the end of the File
      Since:
        1.0

    - #### public void append(Object text, boolean writeBom)

      Append the text at the end of the File. If the default charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) and `writeBom` is `true`, the requisite byte order mark is written to the file before the text.

      Parameters:
        `text` - the text to append at the end of the File
        `writeBom` - whether to write a BOM
      Since:
        2.5.0

    - #### public void append(Object text, String charset)

      Append the text at the end of the File without writing a BOM, using a specified encoding.

      Parameters:
        `text` - the text to append at the end of the File
        `charset` - the charset used
      Since:
        1.0

    - #### public void append(Object text, String charset, boolean writeBom)

      Append the text at the end of the File, using a specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), `writeBom` is `true`, and the file doesn't already exist, the requisite byte order mark is written to the file before the text is appended.

      Parameters:
        `text` - the text to append at the end of the File
        `charset` - the charset used
        `writeBom` - whether to write a BOM
      Since:
        2.5.0

    - #### public Object asType(Class c)

      Converts this File to a Writable or delegates to default DefaultGroovyMethods#asType(java.lang.Object, java.lang.Class).

      Parameters:
        `c` - the desired class
      Returns:
        the converted object
      Since:
        1.0

    - #### public File asWritable()

      Converts this File to a Writable.

      Returns:
        a File which wraps the input file and which implements Writable
      Since:
        1.0

    - #### public File asWritable(String encoding)

      Allows a file to return a Writable implementation that can output itself to a Writer stream.

      Parameters:
        `encoding` - 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

    - #### public static File createTempDir()

    - #### public static File createTempDir(String prefix, String suffix)

      Parameters:

    - #### public boolean deleteDir()

      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
      Returns:
        true if the file doesn't exist or deletion was successful
      Since:
        1.6.0

    - #### public long directorySize()

      Calculates directory size as total size of all its files, recursively.

      Returns:
        directory size (length)
      Since:
        2.1

    - #### public void eachByte(Closure closure)

      Traverse through each byte of this File

      Parameters:
        `closure` - a closure
      Since:
        1.0
      See Also:
        IOGroovyMethods#eachByte(java.io.InputStream, groovy.lang.Closure)

    - #### public void eachByte(int bufferLen, Closure closure)

      Traverse through the bytes of this File, bufferLen bytes at a time.

      Parameters:
        `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.
      Since:
        1.7.4
      See Also:
        IOGroovyMethods#eachByte(java.io.InputStream, int, groovy.lang.Closure)

    - #### public void eachDir(Closure closure)

      Invokes the closure for each subdirectory in this directory, ignoring regular files.

      Parameters:
        `closure` - a closure (the parameter passed is the subdirectory file)
      Since:
        1.0
      See Also:
        File#listFiles()
        File#eachFile(groovy.io.FileType, groovy.lang.Closure)

    - #### public void eachDirMatch(Object nameFilter, Closure closure)

      Invokes the closure for each subdirectory whose name (dir.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object) method to determine if a match occurs. 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:
        `nameFilter` - the nameFilter to perform on the name of the directory (using the DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object) method)
        `closure` - the closure to invoke
      Since:
        1.5.0
      See Also:
        File#eachFileMatch(groovy.io.FileType, java.lang.Object, groovy.lang.Closure)

    - #### public void eachDirRecurse(Closure closure)

      Recursively processes each descendant subdirectory in this directory. Processing consists of calling `closure` passing it the current subdirectory and then recursively processing that subdirectory. Regular files are ignored during traversal.

      Parameters:
        `closure` - a closure
      Since:
        1.5.0
      See Also:
        File#eachFileRecurse(groovy.io.FileType, groovy.lang.Closure)

    - #### public void eachFile(FileType fileType, Closure closure)

      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:
        `fileType` - if normal files or directories or both should be processed
        `closure` - the closure to invoke
      Since:
        1.7.1

    - #### public void eachFile(Closure closure)

      Invokes the closure for each 'child' file in this 'parent' folder/directory. Both regular files and subfolders/subdirectories are processed.

      Parameters:
        `closure` - a closure (the parameter passed is the 'child' file)
      Since:
        1.5.0
      See Also:
        File#listFiles()
        File#eachFile(groovy.io.FileType, groovy.lang.Closure)

    - #### public void eachFileMatch(FileType fileType, Object nameFilter, Closure closure)

      Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods#isCase(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:
        `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(java.lang.Object, java.lang.Object) method)
        `closure` - the closure to invoke
      Since:
        1.7.1

    - #### public void eachFileMatch(Object nameFilter, Closure closure)

      Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods#isCase(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:
        `nameFilter` - the nameFilter to perform on the name of the file (using the DefaultGroovyMethods#isCase(java.lang.Object, java.lang.Object) method)
        `closure` - the closure to invoke
      Since:
        1.5.0
      See Also:
        File#eachFileMatch(groovy.io.FileType, java.lang.Object, groovy.lang.Closure)

    - #### public void eachFileRecurse(FileType fileType, Closure closure)

      Processes each descendant file in this directory and any sub-directories. Processing consists of potentially calling `closure` passing it the current file (which may be a normal file or subdirectory) and then if a subdirectory was encountered, recursively processing the subdirectory. Whether the closure is called is determined by whether the file was a normal file or subdirectory and the value of fileType.

      Parameters:
        `fileType` - if normal files or directories or both should be processed
        `closure` - the closure to invoke on each file
      Since:
        1.7.1

    - #### public void eachFileRecurse(Closure closure)

      Processes each descendant file in this directory and any sub-directories. Processing consists of calling `closure` passing it the current file (which may be a normal file or subdirectory) and then if a subdirectory was encountered, recursively processing the subdirectory.

      Parameters:
        `closure` - a Closure
      Since:
        1.0
      See Also:
        File#eachFileRecurse(groovy.io.FileType, groovy.lang.Closure)

    - #### public Object eachLine(Closure closure)

      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:
        `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
      Since:
        1.5.5
      See Also:
        File#eachLine(int, groovy.lang.Closure)

    - #### public Object eachLine(int firstLine, Closure closure)

      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:
        `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
      Since:
        1.5.7
      See Also:
        IOGroovyMethods#eachLine(java.io.Reader, int, groovy.lang.Closure)

    - #### public Object eachLine(String charset, Closure closure)

      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:
        `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
      Since:
        1.6.8
      See Also:
        File#eachLine(java.lang.String, int, groovy.lang.Closure)

    - #### public Object eachLine(String charset, int firstLine, Closure closure)

      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:
        `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
      Since:
        1.6.8
      See Also:
        IOGroovyMethods#eachLine(java.io.Reader, int, groovy.lang.Closure)

    - #### public void eachObject(Closure closure)

      Iterates through the given file object by object.

      Parameters:
        `closure` - a closure
      Since:
        1.0
      See Also:
        IOGroovyMethods#eachObject(java.io.ObjectInputStream, groovy.lang.Closure)

    - #### public Writable filterLine(Closure closure)

      Filters the lines of a File and creates a Writable in return to stream the filtered lines.

      Parameters:
        `closure` - a closure which returns a boolean indicating to filter the line or not
      Returns:
        a Writable closure
      Since:
        1.0
      See Also:
        IOGroovyMethods#filterLine(java.io.Reader, groovy.lang.Closure)

    - #### public void filterLine(Writer writer, Closure closure)

      Filter the lines from this File, and write them to the given writer based on the given closure predicate.

      Parameters:
        `writer` - a writer destination to write filtered lines to
        `closure` - a closure which takes each line as a parameter and returns `true` if the line should be written to this writer.
      Since:
        1.0
      See Also:
        IOGroovyMethods#filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)

    - #### public void filterLine(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.

      Parameters:
        `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 `true` if the line should be written to this writer.
      Since:
        1.6.8
      See Also:
        IOGroovyMethods#filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)

    - #### public Writable filterLine(String charset, Closure closure)

      Filters the lines of a File and creates a Writable in return to stream the filtered lines.

      Parameters:
        `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
      Since:
        1.6.8
      See Also:
        IOGroovyMethods#filterLine(java.io.Reader, groovy.lang.Closure)

    - #### public byte[] getBytes()

      Read the content of the File and returns it as a byte[].

      Returns:
        a String containing the content of the file
      Since:
        1.7.1

    - #### public String getText()

      Read the content of the File and returns it as a String.

      Returns:
        a String containing the content of the file
      Since:
        1.0

    - #### public String getText(String charset)

      Read the content of the File using the specified encoding and return it as a String.

      Parameters:
        `charset` - the charset used to read the content of the file
      Returns:
        a String containing the content of the file
      Since:
        1.0

    - #### public File leftShift(byte[] bytes)

      Write bytes to a File.

      Parameters:
        `bytes` - the byte array to append to the end of the File
      Returns:
        the original file
      Since:
        1.5.0

    - #### public File leftShift(InputStream data)

      Append binary data to the file. See File#append(java.io.InputStream)

      Parameters:
        `data` - an InputStream of data to write to the file
      Returns:
        the file
      Since:
        1.5.0

    - #### public File leftShift(Object text)

      Write the text to the File.

      Parameters:
        `text` - the text to write to the File
      Returns:
        the original file
      Since:
        1.0

    - #### public DataInputStream newDataInputStream()

      Create a data input stream for this file

      Returns:
        a DataInputStream of the file
      Since:
        1.5.0

    - #### public DataOutputStream newDataOutputStream()

      Creates a new data output stream for this file.

      Returns:
        the created DataOutputStream
      Since:
        1.5.0

    - #### public BufferedInputStream newInputStream()

      Creates a buffered input stream for this file.

      Returns:
        a BufferedInputStream of the file
      Since:
        1.0

    - #### public ObjectInputStream newObjectInputStream()

      Create an object input stream for this file.

      Returns:
        an object input stream
      Since:
        1.5.0

    - #### public ObjectInputStream newObjectInputStream(ClassLoader classLoader)

      Create an object input stream for this file using the given class loader.

      Parameters:
        `classLoader` - the class loader to use when loading the class
      Returns:
        an object input stream
      Since:
        1.5.0

    - #### public ObjectOutputStream newObjectOutputStream()

      Create an object output stream for this file.

      Returns:
        an object output stream
      Since:
        1.5.0

    - #### public BufferedOutputStream newOutputStream()

      Create a buffered output stream for this file.

      Returns:
        the created OutputStream
      Since:
        1.0

    - #### public PrintWriter newPrintWriter()

      Create a new PrintWriter for this file.

      Returns:
        the created PrintWriter
      Since:
        1.0

    - #### public PrintWriter newPrintWriter(String charset)

      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:
        `charset` - the charset
      Returns:
        a PrintWriter
      Since:
        1.0

    - #### public BufferedReader newReader()

      Create a buffered reader for this file.

      Returns:
        a BufferedReader
      Since:
        1.0

    - #### public BufferedReader newReader(String charset)

      Create a buffered reader for this file, using the specified charset as the encoding.

      Parameters:
        `charset` - the charset for this File
      Returns:
        a BufferedReader
      Since:
        1.0

    - #### public BufferedWriter newWriter()

      Create a buffered writer for this file.

      Returns:
        a BufferedWriter
      Since:
        1.0

    - #### public BufferedWriter newWriter(boolean append)

      Creates a buffered writer for this file, optionally appending to the existing file content.

      Parameters:
        `append` - true if data should be appended to the file
      Returns:
        a BufferedWriter
      Since:
        1.0

    - #### public BufferedWriter newWriter(String charset)

      Creates a buffered writer for this file, writing data without writing a BOM, using a specified encoding.

      Parameters:
        `charset` - the name of the encoding used to write in this file
      Returns:
        a BufferedWriter
      Since:
        1.0

    - #### public BufferedWriter newWriter(String charset, boolean append)

      Helper method to create a buffered writer for a file without writing a BOM.

      Parameters:
        `charset` - the name of the encoding used to write in this file
        `append` - true if in append mode
      Returns:
        a BufferedWriter
      Since:
        1.0

    - #### public BufferedWriter newWriter(String charset, boolean append, boolean writeBom)

      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:
        `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
      Since:
        2.5.0

    - #### public byte[] readBytes()

      Reads the content of the file into a byte array.

      Returns:
        a byte array with the contents of the file.
      Since:
        1.0

    - #### public List readLines()

      Reads the file into a list of Strings, with one item for each line.

      Returns:
        a List of lines
      Since:
        1.0
      See Also:
        IOGroovyMethods#readLines(java.io.Reader)

    - #### public List readLines(String charset)

      Reads the file into a list of Strings, with one item for each line.

      Parameters:
        `charset` - opens the file with a specified charset
      Returns:
        a List of lines
      Since:
        1.6.8
      See Also:
        IOGroovyMethods#readLines(java.io.Reader)

    - #### public String relativePath(File to)

      Relative path to file.

      Parameters:
        `to` - the `File` to calculate the path to
      Returns:
        the relative path between the files

    - #### public boolean renameTo(String newPathName)

      Renames the file. It's a shortcut for File#renameTo(File)

      Parameters:
        `newPathName` - The new pathname for the named file
      Returns:
        `true` if and only if the renaming succeeded; `false` otherwise
      Since:
        1.7.4

    - #### public void setBytes(byte[] bytes)

      Write the bytes from the byte array to the File.

      Parameters:
        `bytes` - the byte[] to write to the file
      Since:
        1.7.1

    - #### public void setText(String text)

      Synonym for write(text) allowing file.text = 'foo'.

      Parameters:
        `text` - the text to write to the File
      Since:
        1.5.1
      See Also:
        File#write(java.lang.String)

    - #### public void setText(String text, String charset)

      Synonym for write(text, charset) allowing:

          myFile.setText('some text', charset)

      or with some help from `ExpandoMetaClass`, you could do something like:

          myFile.metaClass.setText = { String s -> delegate.setText(s, 'UTF-8') }
          myfile.text = 'some text'

      Parameters:
        `charset` - The charset used when writing to the file
        `text` - The text to write to the File
      Since:
        1.7.3
      See Also:
        File#write(java.lang.String, java.lang.String)

    - #### public long size()

      Provide the standard Groovy `size()` method for `File`.

      Returns:
        the file's size (length)
      Since:
        1.5.0

    - #### public Object splitEachLine(String regex, Closure closure)

      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:
        `regex` - the delimiting regular expression
        `closure` - a closure
      Returns:
        the last value returned by the closure
      Since:
        1.5.5
      See Also:
        IOGroovyMethods#splitEachLine(java.io.Reader, java.lang.String, groovy.lang.Closure)

    - #### public Object splitEachLine(String regex, String charset, Closure closure)

      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:
        `regex` - the delimiting regular expression
        `charset` - opens the file with a specified charset
        `closure` - a closure
      Returns:
        the last value returned by the closure
      Since:
        1.6.8
      See Also:
        IOGroovyMethods#splitEachLine(java.io.Reader, java.lang.String, groovy.lang.Closure)

    - #### public Object splitEachLine(Pattern pattern, Closure closure)

      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:
        `pattern` - the regular expression Pattern for the delimiter
        `closure` - a closure
      Returns:
        the last value returned by the closure
      Since:
        1.6.8
      See Also:
        IOGroovyMethods#splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)

    - #### public Object splitEachLine(Pattern pattern, String charset, Closure closure)

      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:
        `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
      Since:
        1.6.8
      See Also:
        IOGroovyMethods#splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)

    - #### public void traverse(Closure closure)

      Processes each descendant file in this directory and any sub-directories. Convenience method for File#traverse(java.util.Map, groovy.lang.Closure) when no options to alter the traversal behavior are required.

      Parameters:
        `closure` - the Closure to invoke on each file/directory and optionally returning a FileVisitResult value which can be used to control subsequent processing
      Since:
        1.7.1
      See Also:
        File#traverse(java.util.Map, groovy.lang.Closure)

    - #### public void traverse(Map options)

      Invokes the closure specified with key 'visit' in the options Map for each descendant file in this directory tree. Convenience method for File#traverse(java.util.Map, groovy.lang.Closure) allowing the 'visit' closure to be included in the options Map rather than as a parameter.

      Parameters:
        `options` - a Map of options to alter the traversal behavior
      Since:
        1.7.1
      See Also:
        File#traverse(java.util.Map, groovy.lang.Closure)

    - #### public void traverse(Map options, Closure closure)

      Processes each descendant file in this directory and any sub-directories. Processing consists of potentially calling `closure` passing it the current file (which may be a normal file or subdirectory) and then if a subdirectory was encountered, recursively processing the subdirectory. The traversal can be adapted by providing various options in the `options` Map according to the following keys:

      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 a FileVisitResult 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 a FileVisitResult 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.
      This example prints out file counts and size aggregates for groovy source files within a directory tree:

          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:
        `options` - a Map of options to alter the traversal behavior
        `closure` - the Closure to invoke on each file/directory and optionally returning a FileVisitResult value which can be used to control subsequent processing
      Since:
        1.7.1
      See Also:
        DefaultGroovyMethods#sort(java.util.Collection, groovy.lang.Closure)
        FileVisitResult
        FileType

    - #### public Object withDataInputStream(Closure closure)

      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:
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2
      See Also:
        IOGroovyMethods#withStream(java.io.InputStream, groovy.lang.Closure)

    - #### public Object withDataOutputStream(Closure closure)

      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:
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2
      See Also:
        IOGroovyMethods#withStream(java.io.OutputStream, groovy.lang.Closure)

    - #### public Object withInputStream(Closure closure)

      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:
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2
      See Also:
        IOGroovyMethods#withStream(java.io.InputStream, groovy.lang.Closure)

    - #### public Object withObjectInputStream(Closure closure)

      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:
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2
      See Also:
        IOGroovyMethods#withStream(java.io.InputStream, groovy.lang.Closure)

    - #### public Object withObjectInputStream(ClassLoader classLoader, Closure closure)

      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:
        `classLoader` - the class loader to use when loading the class
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2
      See Also:
        IOGroovyMethods#withStream(java.io.InputStream, groovy.lang.Closure)

    - #### public Object withObjectOutputStream(Closure closure)

      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:
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.0
      See Also:
        IOGroovyMethods#withStream(java.io.OutputStream, groovy.lang.Closure)

    - #### public Object withOutputStream(Closure closure)

      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:
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2
      See Also:
        IOGroovyMethods#withStream(java.io.OutputStream, groovy.lang.Closure)

    - #### public Object withPrintWriter(Closure closure)

      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:
        `closure` - the closure to invoke with the PrintWriter
      Returns:
        the value returned by the closure
      Since:
        1.5.2

    - #### public Object withPrintWriter(String charset, Closure closure)

      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:
        `charset` - the charset
        `closure` - the closure to invoke with the PrintWriter
      Returns:
        the value returned by the closure
      Since:
        1.5.2

    - #### public Object withReader(Closure closure)

      Create a new BufferedReader for this file and then passes it into the closure, ensuring the reader is closed after the closure returns.

      Parameters:
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2

    - #### public Object withReader(String charset, Closure 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.

      Parameters:
        `charset` - the charset for this input stream
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.6.0

    - #### public Object withWriter(Closure 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.

      Parameters:
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2

    - #### public Object withWriter(String charset, Closure closure)

      Creates a new BufferedWriter for this file, passes it to the closure, and ensures the stream is flushed and closed after the closure returns. The writer will use the given charset encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the requisite byte order mark is written to the stream when the writer is created.

      Parameters:
        `charset` - the charset used
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2

    - #### public Object withWriterAppend(Closure closure)

      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:
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2

    - #### public Object withWriterAppend(String charset, Closure closure)

      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:
        `charset` - the charset used
        `closure` - a closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2

    - #### public void write(String text)

      Write the text to the File without writing a BOM.

      Parameters:
        `text` - the text to write to the File
      Since:
        1.0

    - #### public void write(String text, boolean writeBom)

      Write the text to the File. If the default charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) and `writeBom` is `true`, the requisite byte order mark is written to the file before the text.

      Parameters:
        `text` - the text to write to the File
        `writeBom` - whether to write a BOM
      Since:
        2.5.0

    - #### public void write(String text, String charset)

      Write the text to the File without writing a BOM, using the specified encoding.

      Parameters:
        `text` - the text to write to the File
        `charset` - the charset used
      Since:
        1.0

    - #### public void write(String text, String charset, boolean writeBom)

      Write the text to the File, using the specified encoding. If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) and `writeBom` is `true`, the requisite byte order mark is written to the file before the text.

      Parameters:
        `text` - the text to write to the File
        `charset` - the charset used
        `writeBom` - whether to write a BOM
      Since:
        2.5.0