Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Andrey Adamovich - Groovy 2 Cookbook - 2013.pdf
Скачиваний:
44
Добавлен:
19.03.2016
Размер:
26.28 Mб
Скачать

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

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]