From the course: Learning Groovy

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Writing files

Writing files - Groovy Tutorial

From the course: Learning Groovy

Start my 1-month free trial

Writing files

- [Instructor] We learned about reading files with Groovy. Here, we'll want to have a look at the counterpart, writing files. As mentioned earlier, the API of the class java.io.file is a great way to learn more about available methods that help simplify file management. In the previous example, we read person data from a text file. Each line of the text file demarcated a specific portion of information. Let's assume you will want to keep this format but instead of reading the data you will want to also write the data. Groovy makes this process pretty much a breeze. So say you decided to create a new file that should store another person's data. Our first course of action is to create a file instance by pointing it to the output path and the file name. Next, we'll call the method with writer that is supposed to write multiple lines to the file reference encoded in UTF8. Within the closure body, we use the writer instance…

Contents