|
Groovy JDK |
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 Writable or delegates to default Object#asType. |
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. |
boolean
|
deleteDir()
Deletes a directory with all contained files and subdirectories. |
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 Object#isCase method to determine if a match occurs. |
void
|
eachDirRecurse(Closure closure)
Invokes the closure for each descendant directory of 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 Object#isCase 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 Object#isCase method to determine if a match occurs. |
void
|
eachFileRecurse(FileType fileType, Closure closure)
Invokes the closure for each descendant file in this directory. |
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(String charset, 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, 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. |
Writable
|
filterLine(String charset, 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. |
byte[]
|
getBytes()
Read the content of the File and returns it as a byte[]. |
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. |
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. |
List
|
readLines(String charset)
Reads the file into a list of Strings, with one item for each line. |
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(Pattern pattern, Closure closure)
Iterates through this file line by line, splitting each line using the given separator Pattern. |
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, String charset, Closure closure)
Iterates through this file line by line, splitting each line using the given regex separator Pattern. |
void
|
traverse(Map options, Closure closure)
Invokes closure for each descendant file in this directory tree.
|
void
|
traverse(Closure closure)
Invokes the closure for each descendant file in this directory tree. |
void
|
traverse(Map options)
Invokes the closure specified with key 'visit' in the options Map for each descendant file in this directory tree. |
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 |
---|
public void append(Object text)
text
- the text to append at the end of the File.public void append(byte[] bytes)
bytes
- the byte array to append to the end of the File.public void append(InputStream stream)
stream
- stream to read data from..public void append(Object text, String charset)
text
- the text to append at the end of the File.charset
- the charset used.public Object asType(Class c)
c
- the desired class.public File asWritable()
public File asWritable(String encoding)
encoding
- the encoding to be used when reading the file's contents.public boolean deleteDir()
The method returns
public void eachByte(Closure closure)
closure
- a closure.public void eachByte(int bufferLen, Closure closure)
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..public void eachDir(Closure closure)
closure
- a closure (first parameter is the subdirectory file).public void eachDirMatch(Object nameFilter, Closure closure)
nameFilter
- the nameFilter to perform on the name of the directory (using the Object#isCase method).closure
- the closure to invoke.public void eachDirRecurse(Closure closure)
closure
- a closure.public void eachFile(FileType fileType, Closure closure)
fileType
- if normal files or directories or both should be processed.closure
- the closure to invoke.public void eachFile(Closure closure)
closure
- a closure (first parameter is the 'child' file).public void eachFileMatch(FileType fileType, Object nameFilter, Closure closure)
// 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()}" }
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 Object#isCase method).closure
- the closure to invoke.public void eachFileMatch(Object nameFilter, Closure closure)
nameFilter
- the nameFilter to perform on the name of the file (using the Object#isCase method).closure
- the closure to invoke.public void eachFileRecurse(FileType fileType, Closure closure)
fileType
- if normal files or directories or both should be processed.closure
- the closure to invoke on each file.public void eachFileRecurse(Closure closure)
closure
- a closure.public Object eachLine(Closure closure)
closure
- a closure (arg 1 is line, optional arg 2 is line number starting at line 1).public Object eachLine(String charset, Closure closure)
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).public Object eachLine(int firstLine, Closure closure)
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).public Object eachLine(String charset, int firstLine, Closure closure)
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).public void eachObject(Closure closure)
closure
- a closure.public Writable filterLine(Closure closure)
closure
- a closure which returns a boolean indicating to filter
the line or not.public Writable filterLine(String charset, Closure closure)
charset
- opens the file with a specified charset.closure
- a closure which returns a boolean indicating to filter
the line or not.public void filterLine(Writer writer, Closure closure)
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..public void filterLine(Writer writer, String charset, Closure closure)
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..public byte[] getBytes()
public String getText(String charset)
charset
- the charset used to read the content of the file.public String getText()
public File leftShift(Object text)
text
- the text to write to the File.public File leftShift(byte[] bytes)
bytes
- the byte array to append to the end of the File.public File leftShift(InputStream data)
data
- an InputStream of data to write to the file.public DataInputStream newDataInputStream()
public DataOutputStream newDataOutputStream()
public BufferedInputStream newInputStream()
public ObjectInputStream newObjectInputStream()
public ObjectInputStream newObjectInputStream(ClassLoader classLoader)
classLoader
- the class loader to use when loading the class.public ObjectOutputStream newObjectOutputStream()
public BufferedOutputStream newOutputStream()
public PrintWriter newPrintWriter()
public PrintWriter newPrintWriter(String charset)
charset
- the charset.public BufferedReader newReader()
public BufferedReader newReader(String charset)
charset
- the charset for this File.public BufferedWriter newWriter()
public BufferedWriter newWriter(boolean append)
append
- true if data should be appended to the file.public BufferedWriter newWriter(String charset, boolean append)
charset
- the name of the encoding used to write in this file.append
- true if in append mode.public BufferedWriter newWriter(String charset)
charset
- the name of the encoding used to write in this file.public byte[] readBytes()
public List readLines()
public List readLines(String charset)
charset
- opens the file with a specified charset.public boolean renameTo(String newPathName)
newPathName
- The new pathname for the named file.true
if and only if the renaming succeeded;
false
otherwisepublic void setBytes(byte[] bytes)
bytes
- the byte[] to write to the file.public void setText(String text)
text
- the text to write to the File.public void setText(String text, String charset)
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'
charset
- The charset used when writing to the file.text
- The text to write to the File.public long size()
size()
method for File
.public Object splitEachLine(String regex, Closure closure)
regex
- the delimiting regular expression.closure
- a closure.public Object splitEachLine(Pattern pattern, Closure closure)
pattern
- the regular expression Pattern for the delimiter.closure
- a closure.public Object splitEachLine(String regex, String charset, Closure closure)
regex
- the delimiting regular expression.charset
- opens the file with a specified charset.closure
- a closure.public Object splitEachLine(Pattern pattern, String charset, Closure closure)
pattern
- the regular expression Pattern for the delimiter.charset
- opens the file with a specified charset.closure
- a closure.public void traverse(Map options, Closure closure)
closure
for each descendant file in this directory tree.
Sub-directories are recursively traversed as found.
The traversal can be adapted by providing various options in the options
Map according
to the following keys: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++ }
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.public void traverse(Closure closure)
closure
- the Closure to invoke on each file/directory and optionally returning a FileVisitResult value
which can be used to control subsequent processing.public void traverse(Map options)
options
- a Map of options to alter the traversal behavior.public Object withDataInputStream(Closure closure)
closure
- a closure.public Object withDataOutputStream(Closure closure)
closure
- a closure.public Object withInputStream(Closure closure)
closure
- a closure.public Object withObjectInputStream(Closure closure)
closure
- a closure.public Object withObjectInputStream(ClassLoader classLoader, Closure closure)
classLoader
- the class loader to use when loading the class.closure
- a closure.public Object withObjectOutputStream(Closure closure)
closure
- a closure.public Object withOutputStream(Closure closure)
closure
- a closure.public Object withPrintWriter(Closure closure)
closure
- the closure to invoke with the PrintWriter.public Object withPrintWriter(String charset, Closure closure)
charset
- the charset.closure
- the closure to invoke with the PrintWriter.public Object withReader(Closure closure)
closure
- a closure.public Object withReader(String charset, Closure closure)
charset
- the charset for this input stream.closure
- a closure.public Object withWriter(Closure closure)
closure
- a closure.public Object withWriter(String charset, Closure closure)
charset
- the charset used.closure
- a closure.public Object withWriterAppend(String charset, Closure closure)
charset
- the charset used.closure
- a closure.public Object withWriterAppend(Closure closure)
closure
- a closure.public void write(String text)
text
- the text to write to the File.public void write(String text, String charset)
text
- the text to write to the File.charset
- the charset used.
|
Groovy JDK |