- •Credits
- •About the Authors
- •About the Reviewers
- •www.PacktPub.com
- •Table of Contents
- •Preface
- •Introduction
- •Installing Groovy on Windows
- •Installing Groovy on Linux and OS X
- •Executing Groovy code from the command line
- •Using Groovy as a command-line text file editor
- •Running Groovy with invokedynamic support
- •Building Groovy from source
- •Managing multiple Groovy installations on Linux
- •Using groovysh to try out Groovy commands
- •Starting groovyConsole to execute Groovy snippets
- •Configuring Groovy in Eclipse
- •Configuring Groovy in IntelliJ IDEA
- •Introduction
- •Using Java classes from Groovy
- •Embedding Groovy into Java
- •Compiling Groovy code
- •Generating documentation for Groovy code
- •Introduction
- •Searching strings with regular expressions
- •Writing less verbose Java Beans with Groovy Beans
- •Inheriting constructors in Groovy classes
- •Defining code as data in Groovy
- •Defining data structures as code in Groovy
- •Implementing multiple inheritance in Groovy
- •Defining type-checking rules for dynamic code
- •Adding automatic logging to Groovy classes
- •Introduction
- •Reading from a file
- •Reading a text file line by line
- •Processing every word in a text file
- •Writing to a file
- •Replacing tabs with spaces in a text file
- •Deleting a file or directory
- •Walking through a directory recursively
- •Searching for files
- •Changing file attributes on Windows
- •Reading data from a ZIP file
- •Reading an Excel file
- •Extracting data from a PDF
- •Introduction
- •Reading XML using XmlSlurper
- •Reading XML using XmlParser
- •Reading XML content with namespaces
- •Searching in XML with GPath
- •Searching in XML with XPath
- •Constructing XML content
- •Modifying XML content
- •Sorting XML nodes
- •Serializing Groovy Beans to XML
- •Introduction
- •Parsing JSON messages with JsonSlurper
- •Constructing JSON messages with JsonBuilder
- •Modifying JSON messages
- •Validating JSON messages
- •Converting JSON message to XML
- •Converting JSON message to Groovy Bean
- •Using JSON to configure your scripts
- •Introduction
- •Creating a database table
- •Connecting to an SQL database
- •Modifying data in an SQL database
- •Calling a stored procedure
- •Reading BLOB/CLOB from a database
- •Building a simple ORM framework
- •Using Groovy to access Redis
- •Using Groovy to access MongoDB
- •Using Groovy to access Apache Cassandra
- •Introduction
- •Downloading content from the Internet
- •Executing an HTTP GET request
- •Executing an HTTP POST request
- •Constructing and modifying complex URLs
- •Issuing a REST request and parsing a response
- •Issuing a SOAP request and parsing a response
- •Consuming RSS and Atom feeds
- •Using basic authentication for web service security
- •Using OAuth for web service security
- •Introduction
- •Querying methods and properties
- •Dynamically extending classes with new methods
- •Overriding methods dynamically
- •Adding performance logging to methods
- •Adding transparent imports to a script
- •DSL for executing commands over SSH
- •DSL for generating reports from logfiles
- •Introduction
- •Processing collections concurrently
- •Downloading files concurrently
- •Splitting a large task into smaller parallel jobs
- •Running tasks in parallel and asynchronously
- •Using actors to build message-based concurrency
- •Using STM to atomically update fields
- •Using dataflow variables for lazy evaluation
- •Index
Working with Files in Groovy
Introduction
Groovy offers many shortcuts for dealing with files and directories. Mundane tasks such as listing, copying, renaming, and deleting files are elegantly executed by Groovy, thanks to the methods added to the standard JDK classes such as java.io.File. This chapter's recipes are all about I/O with Groovy, from simple cases such as reading a file to more complex endeavors, such as mining data from a PDF file or an Excel spreadsheet. The Java build tool
Ant makes a cameo appearance in this chapter as well. Groovy and Ant are tightly integrated and we will discover some of the tasks made available through this marriage.
Reading from a file
Groovy makes dealing with files a very pleasant business, thanks to a number of helper methods that work with standard Java's Reader and Writer, InputStream and
OutputStream and File classes.
In this recipe, we are going to look at the options available for reading a file in Groovy and accessing its content, mainly through the Groovy-enhanced File class.
Getting ready
Let's assume we have a script that defines the following java.io.File object:
def file = new File('poem.txt')
Of course, in order to be read, the poem.txt file needs to exist in the same directory where our script is.
How to do it...
Let's explore some different ways to read the content of the poem.txt file.
1.In order to get the full text file content as a java.lang.String, you can use the getText method provided by the Groovy JDK extension:
String textContent = file.text
file.text is equivalent to file.getText, thanks to Groovy's special property getter handling syntax.
2.You can also read it into memory as a byte array by using the getBytes method: byte[] binaryContent = file.bytes
136
www.it-ebooks.info
Chapter 4
3.If you want to have more control over the reading process, then you can use the withReader method, which takes a closure as an input parameter:
file.withReader { Reader reader -> def firstLine = reader.readLine() println firstLine
}
4.In a similar way, you can read the file with the help of the withInputStream method, which is similar to withReader with the exception that the closure operates on a java.io.BufferedInputStream instance:
file.withInputStream { InputStream stream -> def firstByte = stream.read()
println firstByte
}
How it works...
The getText method displayed in step 1 can also be called by passing the charset name, in order to specify the character encoding:
String textContent = file.getText('UTF-8')
The withReader method in step 3 is an example of how Groovy can make the code much more concise and unceremonious. In Java, readers and writers have to be explicitly closed after we finish with them. Groovy has several with... methods for File, URL, streams, readers and writers, where ... is the name of the stream, reader or writer class. We pass a closure to these methods and Groovy makes sure all streams, readers and writers are closed correctly, even if exceptions are thrown in the closure (Java 7 now has a similar feature via
the java.lang.AutoCloseable interface). The withReader method creates a new java.io.BufferedReader instance for the given file and passes the reader into the closure. It also ensures that the reader object gets closed after the method returns.
There's more...
To get even more control, you can just use the newReader method to get the reader:
Reader reader = file.newReader()
Or the newInputStream method to get the stream:
InputStream stream = file.newInputStream()
Naturally, that means that you need to manually control the resource life cycle and close them properly.
137
www.it-ebooks.info
Working with Files in Groovy
The Groovy's extensions for java.io.File also offer methods to create standard Java input streams. You can use newDataInputStream or newObjectInputStream to create streams for reading Java primitives and serialized Java objects. To get those streams automatically closed you can also use the withDataInputStream and
withObjectInputStream methods.
All the methods described above shorten your path to reading a file's content compared to what you would need in pure Java.
Additionally, many of the methods (for example, getText, getBytes, and others described in later recipes) that we find on java.io.File are also available in java.io.Reader and java.io.InputStream Groovy extensions.
See also
ff Reading a text file line by line
ff Processing every word in a text file ff Writing to a file
ff http://docs.oracle.com/javase/6/docs/api/java/io/File.html
ff http://groovy.codehaus.org/groovy-jdk/java/io/File.html
ff http://groovy.codehaus.org/groovy-jdk/java/io/Reader.html
ff http://groovy.codehaus.org/groovy-jdk/java/io/InputStream.html
Reading a text file line by line
You may often find yourself with the need to read a file line-by-line and extract information from each processed line; think of a logfile. In this recipe, we will learn a quick way to read text files line-by-line using the efficient Groovy I/O APIs.
Getting ready
For the following code snippets, please refer to the Getting Ready section in the Reading from a file recipe, or just assume you have the file variable of the java.io.File type defined somewhere in your script.
How to do it...
Let's see how to read all the text file's lines and echo them to the standard output.
1.To read all the lines at once, you can use the readLines method: def lines = file.readLines()
138
www.it-ebooks.info
Chapter 4
2.The lines is a collection (java.util.ArrayList) that you can iterate over with the usual collection iterator:
lines.each { String line -> println line
}
3.There is also the eachLine method, which allows doing the above without keeping an intermediate variable:
file.eachLine { String line -> println line
}
There's more...
The java.io.Reader and java.io.InputStream class extensions also have the readLines and eachLine methods. For example, consider the following code:
file.withReader { Reader reader -> reader.eachLine { String line ->
...
}
}
This actually makes it possible for any Reader or InputStream to be processed line-by-line.
In a similar way, you can also read files, readers, or streams byte by byte with the help of the eachByte method:
file.eachByte { int b ->
...
}
See also
The Processing every word in a text file recipe contains additional approaches to a text file content processing.
You can also find the following Javadoc and Groovydoc references useful:
ff http://docs.oracle.com/javase/6/docs/api/java/io/File.html
ff http://groovy.codehaus.org/groovy-jdk/java/io/File.html
ff http://groovy.codehaus.org/groovy-jdk/java/io/Reader.html
ff http://groovy.codehaus.org/groovy-jdk/java/io/InputStream.html
139
www.it-ebooks.info