Overview | Package | Class | Tree | Deprecated | Index | Help
| Groovy JDK

## java.io
Class File

Method Summary
`void` | `append(Object text)` Append the text at the end of the File.
`void` | `append(byte[] bytes)` Append bytes to the end of a File.
`void` | `append(InputStream stream)` Append binary data to the file.
`void` | `append(Object text, String charset)` Append the text at the end of the File, using a specified encoding.
`Object` | `asType(Class c)` Converts this File to a {@link Writable} or delegates to default {@link #asType(Object,Class)}.
`File` | `asWritable()` Converts this File to a {@link Writable}.
`File` | `asWritable(String encoding)` Allows a file to return a Writable implementation that can output itself to a Writer stream.
`boolean` | `deleteDir()` Deletes a directory with all contained files and subdirectories.
`void` | `eachByte(Closure closure)` Traverse through each byte of this File
`void` | `eachDir(Closure closure)` Invokes the closure for each subdirectory in this directory, ignoring regular files.
`void` | `eachDirMatch(Object filter, Closure closure)` Invokes the closure for each subdirectory whose name (dir.name) matches the given filter in the given directory - calling the isCase() method to determine if a match occurs.
`void` | `eachDirRecurse(Closure closure)` Invokes the closure for each descendant directory of this directory.
`void` | `eachFile(Closure closure)` Invokes the closure for each 'child' file in this 'parent' folder/directory.
`void` | `eachFileMatch(Object filter, Closure closure)` Invokes the closure for each file whose name (file.name) matches the given filter in the given directory - calling the isCase() method to determine if a match occurs.
`void` | `eachFileRecurse(Closure closure)` Invokes the closure for each descendant file in this directory.
`Object` | `eachLine(Closure closure)` Iterates through this file line by line.
`Object` | `eachLine(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 Writeable 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.
`String` | `getText(String charset)` Read the content of the File using the specified encoding and return it as a String.
`String` | `getText()` Read the content of the File and returns it as a String.
`Iterator` | `iterator()` Just throws a DeprecationException.
`File` | `leftShift(Object text)` Write the text to the File.
`File` | `leftShift(byte[] bytes)` Write bytes to a File.
`File` | `leftShift(InputStream data)` Append binary data 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, boolean append)` Helper method to create a buffered writer for a file.
`BufferedWriter` | `newWriter(String charset)` Creates a buffered writer for this file, writing data using the given encoding.
`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.
`void` | `setText(String text)` Synonym for write(text) allowing file.text = 'foo'.
`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` | `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(String charset, Closure closure)` Create a new BufferedWriter which will append to this file.
`Object` | `withWriterAppend(Closure closure)` Create a new BufferedWriter for this file in append mode.
`void` | `write(String text)` Write the text to the File.
`void` | `write(String text, String charset)` Write the text to the File, using the specified encoding.

Method Detail

### append

    public void append(Object text)

  Append the text at the end of the File.

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

Since:
  1.0

### append

    public void append(byte[] bytes)

  Append bytes to the end of a File.

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

Since:
  1.5.1

### append

    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

### append

    public void append(Object text, String charset)

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

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

Since:
  1.0

### asType

    public Object asType(Class c)

  Converts this File to a {@link Writable} or delegates to default {@link #asType(Object,Class)}.

Parameters:
  `c` - the desired class.

Returns:
  the converted object

Since:
  1.0

### asWritable

    public File asWritable()

  Converts this File to a {@link Writable}.

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

Since:
  1.0

### asWritable

    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

### deleteDir

    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

### eachByte

    public void eachByte(Closure closure)

  Traverse through each byte of this File

Parameters:
  `closure` - a closure.

Since:
  1.0

See:
  #eachByte(InputStream,Closure).

### eachDir

    public void eachDir(Closure closure)

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

Parameters:
  `closure` - a closure (first parameter is the subdirectory file).

Since:
  1.0

See:
  #eachFile(File, Closure).

### eachDirMatch

    public void eachDirMatch(Object filter, Closure closure)

  Invokes the closure for each subdirectory whose name (dir.name) matches the given filter in the given directory - calling the isCase() 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:
  `filter` - the filter to perform on the directory (using the isCase(object) method).
  `closure` - the closure to invoke.

Since:
  1.5.0

### eachDirRecurse

    public void eachDirRecurse(Closure closure)

  Invokes the closure for each descendant directory of this directory. Sub-directories are recursively searched in a depth-first fashion. Only subdirectories are passed to the closure; regular files are ignored.

Parameters:
  `closure` - a closure.

Since:
  1.5.0

See:
  #eachFileRecurse(File,Closure,boolean).

### eachFile

    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 (first parameter is the 'child' file).

Since:
  1.5.0

See:
  File#listFiles().
  #eachDir(File, Closure).

### eachFileMatch

    public void eachFileMatch(Object filter, Closure closure)

  Invokes the closure for each file whose name (file.name) matches the given filter in the given directory - calling the isCase() method to determine if a match occurs. This method can be used with different kinds of filters like regular expressions, classes, ranges etc. Both regular files and subdirectories are matched.

Parameters:
  `filter` - the filter to perform on the directory (using the isCase(object) method).
  `closure` - the closure to invoke.

Since:
  1.5.0

### eachFileRecurse

    public void eachFileRecurse(Closure closure)

  Invokes the closure for each descendant file in this directory. Sub-directories are recursively searched in a depth-first fashion. Both regular files and subdirectories are passed to the closure.

Parameters:
  `closure` - a closure.

Since:
  1.0

### eachLine

    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:
  #eachLine.

### eachLine

    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 count of the first line.
  `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:
  #eachLine.

### eachObject

    public void eachObject(Closure closure)

  Iterates through the given file object by object.

Parameters:
  `closure` - a closure.

Since:
  1.0

See:
  #eachObject(ObjectInputStream,Closure).

### filterLine

    public Writable filterLine(Closure closure)

  Filters the lines of a File and creates a Writeable 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:
  #filterLine(Reader,Closure).

### filterLine

    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:
  #filterLine(Reader,Writer,Closure).

### getText

    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

### getText

    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

### iterator

    public Iterator iterator()

  Just throws a DeprecationException. DO NOT USE. It used to provide an iterator for text file content one line at a time.

Returns:
  a line-based iterator

Since:
  1.5.0

### leftShift

    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

### leftShift

    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

### leftShift

    public File leftShift(InputStream data)

  Append binary data to the file. See {@link #append(File, InputStream)}

Parameters:
  `data` - an InputStream of data to write to the file.

Returns:
  the file

Since:
  1.5.0

### newDataInputStream

    public DataInputStream newDataInputStream()

  Create a data input stream for this file

Returns:
  a DataInputStream of the file

Since:
  1.5.0

### newDataOutputStream

    public DataOutputStream newDataOutputStream()

  Creates a new data output stream for this file.

Returns:
  the created DataOutputStream

Since:
  1.5.0

### newInputStream

    public BufferedInputStream newInputStream()

  Creates a buffered input stream for this file.

Returns:
  a BufferedInputStream of the file

Since:
  1.0

### newObjectInputStream

    public ObjectInputStream newObjectInputStream()

  Create an object input stream for this file.

Returns:
  an object input stream

Since:
  1.5.0

### newObjectInputStream

    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

### newObjectOutputStream

    public ObjectOutputStream newObjectOutputStream()

  Create an object output stream for this file.

Returns:
  an object output stream

Since:
  1.5.0

### newOutputStream

    public BufferedOutputStream newOutputStream()

  Create a buffered output stream for this file.

Returns:
  the created OutputStream

Since:
  1.0

### newPrintWriter

    public PrintWriter newPrintWriter()

  Create a new PrintWriter for this file.

Returns:
  the created PrintWriter

Since:
  1.0

### newPrintWriter

    public PrintWriter newPrintWriter(String charset)

  Create a new PrintWriter for this file, using specified charset.

Parameters:
  `charset` - the charset.

Returns:
  a PrintWriter

Since:
  1.0

### newReader

    public BufferedReader newReader()

  Create a buffered reader for this file.

Returns:
  a BufferedReader

Since:
  1.0

### newReader

    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

### newWriter

    public BufferedWriter newWriter()

  Create a buffered writer for this file.

Returns:
  a BufferedWriter

Since:
  1.0

### newWriter

    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

### newWriter

    public BufferedWriter newWriter(String charset, boolean append)

  Helper method to create a buffered writer for a file. If the given charset is "UTF-16BE" or "UTF-16LE", 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.

Returns:
  a BufferedWriter

Since:
  1.0

### newWriter

    public BufferedWriter newWriter(String charset)

  Creates a buffered writer for this file, writing data using the given encoding.

Parameters:
  `charset` - the name of the encoding used to write in this file.

Returns:
  a BufferedWriter

Since:
  1.0

### readBytes

    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

### readLines

    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

### setText

    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:
  #write(File, String).

### size

    public long size()

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

Returns:
  the file's size (length)

Since:
  1.5.0

### splitEachLine

    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:
  #splitEachLine(Reader,String,Closure).

### withDataInputStream

    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:
  #withStream(InputStream,Closure).

### withDataOutputStream

    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:
  #withStream(OutputStream,Closure).

### withInputStream

    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:
  #withStream(InputStream,Closure).

### withObjectInputStream

    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:
  #withStream(InputStream,Closure).

### withObjectInputStream

    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:
  #withStream(InputStream,Closure).

### withObjectOutputStream

    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:
  #withStream(OutputStream,Closure).

### withOutputStream

    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:
  #withStream(OutputStream,Closure).

### withPrintWriter

    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

### withPrintWriter

    public Object withPrintWriter(String charset, Closure closure)

  Create a new PrintWriter with a specified charset for this file. The writer is passed to the closure, and will be closed before this method returns.

Parameters:
  `charset` - the charset.
  `closure` - the closure to invoke with the PrintWriter.

Returns:
  the value returned by the closure

Since:
  1.5.2

### withReader

    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

### withReader

    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

### withWriter

    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

### withWriter

    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.

Parameters:
  `charset` - the charset used.
  `closure` - a closure.

Returns:
  the value returned by the closure

Since:
  1.5.2

### withWriterAppend

    public Object withWriterAppend(String charset, Closure closure)

  Create a new BufferedWriter which will append to this file. The writer is passed to the closure and will be closed before this method returns.

Parameters:
  `charset` - the charset used.
  `closure` - a closure.

Returns:
  the value returned by the closure

Since:
  1.5.2

### withWriterAppend

    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

### write

    public void write(String text)

  Write the text to the File.

Parameters:
  `text` - the text to write to the File.

Since:
  1.0

### write

    public void write(String text, String charset)

  Write the text to the File, using the specified encoding.

Parameters:
  `text` - the text to write to the File.
  `charset` - the charset used.

Since:
  1.0

Overview | Package | Class | Tree | Deprecated | Index | Help
| Groovy JDK