Book Image

Groovy 2 Cookbook

Book Image

Groovy 2 Cookbook

Overview of this book

Get up to speed with Groovy, a language for the Java Virtual Machine (JVM) that integrates features of both object-oriented and functional programming. This book will show you the powerful features of Groovy 2 applied to real-world scenarios and how the dynamic nature of the language makes it very simple to tackle problems that would otherwise require hours or days of research and implementation. Groovy 2 Cookbook contains a vast number of recipes covering many facets of today's programming landscape. From language-specific topics such as closures and metaprogramming, to more advanced applications of Groovy flexibility such as DSL and testing techniques, this book gives you quick solutions to everyday problems. The recipes in this book start from the basics of installing Groovy and running your first scripts and continue with progressively more advanced examples that will help you to take advantage of the language's amazing features. Packed with hundreds of tried-and-true Groovy recipes, Groovy 2 Cookbook includes code segments covering many specialized APIs to work with files and collections, manipulate XML, work with REST services and JSON, create asynchronous tasks, and more. But Groovy does more than just ease traditional Java development: it brings modern programming features to the Java platform like closures, duck-typing, and metaprogramming. In this new book, you'll find code examples that you can use in your projects right away along with a discussion about how and why the solution works. Focusing on what's useful and tricky, Groovy 2 Cookbook offers a wealth of useful code for all Java and Groovy programmers, not just advanced practitioners.
Table of Contents (17 chapters)
Groovy 2 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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()
  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: