1. Eachline--open and read each line of the file
New File ("Foo.txt"). Eachline { println it.touppercase ();}
2. ReadLines-The function is basically the same as the eachline, but it does not accept the closure as a parameter, but instead reads the file line into a list
New File ("Foo.txt"). ReadLines (); Linelist.each { println it.touppercase ();}
3. Spliteachline-Reads each line of the file and then splits the array into rows with the specified delimiter. Needless to say, this method is quite efficient for working with CSV files.
New File ("Foo.csv"). Spliteachline (",") { "name=${it[0]} Balance=${it[1]}";
4. Eachbyte--processing binary files, accessing files at byte level, this method is equivalent to the Eachline () method.
New File ("Foo.bin"). eachbyte {print it;}
5. Readbytes-Natural, processing binary files, byte-level access to files, this method is equivalent to the ReadLines () method
New File ("Foo.bin"). Readbytes (); Bytelist.each { println it;}
6. Write-Groovy It's so intuitive to write files in this way.
New File ("Foo.txt"). Write ("Testing testing"); New File ("Foo.txt"). Write ("" "isjust a test fileto playwith" "");
The above uses the triple reference syntax, where the text is preserved in a format written to the file. Note that the above text will have a blank line at the end of the file, unless the starting and ending characters are close to "" ", and the above method to write the file with the word open will be squeezed in a row, open with EditPlus is more than one line, because it is under the Linux/n newline, not Windows/r/n line 。 、
7. Append--Unlike write overwrite files, append is appended to the file
New File ("Foo.txt"). Append ("" "/This is just a test file to play Withff" "" );
8. Eachfile--functionally similar to the Java.io.File Listfiles () method. Used to enumerate each file in the path (including the directory) and pass it to the closure processing
New File ("."). Eachfile { // The File here represents a path println it.getname (); // eachfile () each item listed is a File instance }
9. Eachfilerecurse--recursively traverse paths in a depth-first manner, listing files (including directories) and passing them to the closure processing
New File ("."). Eachfilerecurse { // The File here represents a path println it.getpath (); // eachfile () each item listed is a File instance }
10 ..... Again, please refer to http://groovy.codehaus.org/groovy-jdk/java/io/File.html for other Groovy extension methods for Java.io.File. such as Eachdir (), Eachdirmatch (), Eachdirrecurse (), Eachfilematch (), Filterline (), Newinputstream (), Newoutputstream (), Newreader (), Newprintwriter (), Withinputstream (), Withoutputstream (), Withreader (), Withprintwriter (), and so on. Also note that there are ways to specify a character set.
----
Operating Directory
List all files in the directory (including subfolders, files within subfolders):
New File (dirName) if (Dir.isdirectory ()) { - println file } } dir.eachfilematch (~/.*\.txt/) {file it-> println it.name // make regular expression match file name dir.eachfilematch (FILES, ~/.*\.txt/) {file it-> println It.name }
Write a file
Import java.io.File def writefile (filename) { new File (filename) if ( File.exists ()) file.delete () // printwriter.write (' the first Content of file ') printwriter.write (' \ n ') printwriter.write (' The first content of File ') Printwriter.flush () printwriter.close () }
In addition file.newPrintWriter()
to getting a printwriter, a similar approach also has file.newInputStream()
,
file.newObjectInputStream()
such as
More concise wording:
New File (fileName). withprintwriter {printwriter- printwriter.println (' The first content of file ') }
Parsing XML files
<?XML version= "1.0" encoding= "UTF-8"?> <Customers> <Corporate> <Customername= "Bill Gates" Company= "Microsoft"></Customer> <Customername= "Steve Jobs" Company= "Apple"></Customer> <Customername= "Bill Dyh" Company= "Sun"></Customer> </Corporate> <Consumer> <Customername= "Jone Doe"></Customer> <Customername= "Jane Doe"></Customer> </Consumer> </Customers>
New Xmlslurper (). Parse (new file ("customers.xml"/* * parse the files */For (Customer in Customers.corporate.customer) { "${[email protected]} works For${[email protected] }"
Parsing propeties files
Refer to the Groovy:how to access to properties file?, the code is as follows:
New Properties () New File ("Message.properties"). Withinputstream { -Props.load (Stream)}// Accessing the property from Properties object using Groovy ' s map notationprintln "capacity.created=" + props["capacit y.created "new configslurper (). Parse (props)// accessing the property From Configslurper object using Gpath expressionprintln "capacity.created=" + config.capacity.created
Another way:
New Configslurper (). Parse (new File ("Message.groovy"). Tourl ())
Message.groovy content is as follows:
capacity { created= "x" modified= "Y"}
Groovy Reads file information