Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Groovy 2 Cookbook

You're reading from   Groovy 2 Cookbook Java and Groovy go together like ham and eggs, and this book is a great opportunity to learn how to exploit Groovy 2 to the full. Packed with recipes, both intermediate and advanced, it's a great way to speed up and modernize your programming.

Arrow left icon
Product type Paperback
Published in Oct 2013
Publisher Packt
ISBN-13 9781849519366
Length 394 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Luciano Fiandesio Luciano Fiandesio
Author Profile Icon Luciano Fiandesio
Luciano Fiandesio
Andrey Adamovich Andrey Adamovich
Author Profile Icon Andrey Adamovich
Andrey Adamovich
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Groovy 2 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with Groovy 2. Using Groovy Ecosystem FREE CHAPTER 3. Using Groovy Language Features 4. Working with Files in Groovy 5. Working with XML in Groovy 6. Working with JSON in Groovy 7. Working with Databases in Groovy 8. Working with Web Services in Groovy 9. Metaprogramming and DSLs in Groovy 10. Concurrent Programming in Groovy 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:

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime