Chapter 4. Working with Files in Groovy

In this chapter, we will cover:

  • 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
  • Filtering a text file's content
  • 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

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.29.145