groovy write to file

Groovy provides a number of helper methods when working with I/O. Groovy provides easier classes to provide the following functionalities for files.

  • Reading files
  • Writing to files
  • Traversing file trees
  • Reading and writing data objects to files

In addition to this, you can always use the normal Java classes listed below for File I/O operations.

  • java.io.File
  • java.io.InputStream
  • java.io.OutputStream
  • java.io.Reader
  • java.io.Writer

Reading files

The following example will output all the lines of a text file in Groovy. The method eachLine is in-built in the File class in Groovy for the purpose of ensuring that each line of the text file is read.

The File class is used to instantiate a new object which takes the file name as the parameter. It then takes the function of eachLine, puts it to a variable called line and prints it accordingly.

If the file contains the following lines, they will be printed.

Reading the Contents of a File as an Entire String

If you want to get the entire contents of the file as a string, you can use the text property of the file class. The following example shows how this can be done.

If the file contains the following lines, they will be printed.

Writing to Files

If you want to write to files, you need to use the writer class to output text to a file. The following example shows how this can be done.

If you open the file Example.txt, you will see the words “Hello World” printed to the file.

Getting the Size of a File

If you want to get the size of the file one can use the length property of the file class to get the size of the file. The following example shows how this can be done.

The above code would show the size of the file in bytes.

Testing if a File is a Directory

If you want to see if a path is a file or a directory, one can use the isFile and isDirectory option of the File class. The following example shows how this can be done.

Читайте также:  1с получить остатки по регистратору

The above code would show the following output −

Creating a Directory

If you want to create a new directory you can use the mkdir function of the File class. The following example shows how this can be done.

The directory E:Directory will be created if it does not exist.

Deleting a File

If you want to delete a file you can use the delete function of the File class. The following example shows how this can be done.

The file will be deleted if it exists.

Copying files

Groovy also provides the functionality to copy the contents from one file to another. The following example shows how this can be done.

The file Example1.txt will be created and all of the contents of the file Example.txt will be copied to this file.

Getting Directory Contents

Groovy also provides the functionality to list the drives and files in a drive.

The following example shows how the drives on a machine can be displayed by using the listRoots function of the File class.

Depending on the drives available on your machine, the output could vary. On a standard machine the output would be similar to the following one −

The following example shows how to list the files in a particular directory by using the eachFile function of the File class.

The output would display all of the files in the directory E:Temp

If you want to recursively display all of files in a directory and its subdirectories, then you would use the eachFileRecurse function of the File class. The following example shows how this can be done.

The output would display all of the files in the directory E:Temp and in its subdirectories if they exist.

Beginners Tutorial

Example Applications

If you like my posts, like my Facebook page please 🙂

Groovy File Examples

May 01, 2015 Groovy, Tutorial comments

File processing is one of the most common task in programming. Fortunately, Groovy makes file manipulation easier. Things that are long and complicated in Java are now very much concise in Groovy. Here are some Groovy File programming examples.

Introduction

The beauty of working with files in groovy is not needing to work with so many different interfaces and classes. For example, we don’t need this code to read a file:
There is just too many classes to study in Java to do different kinds of file manipulation. Groovy provides a much simpler way to work with files. You just need to create the file object and you are good to go!
Most of what we need to do are supported in this class. No need to combine with other classes. Below are some examples on how to do file manipulation in Groovy.

Читайте также:  canon selphy cp1000 white

Get File Size

Getting the file size of a Groovy file is easy. You just need to invoke the length() method of a Groovy File object. Here is an example code:

Here is a sample output on my computer:

Read Contents As String

Reading the contents of a Groovy file as a String is supported by the property text. You just need to read this property to get the contents of the entire file. Here is an example code that reads the content of a Groovy file and prints in to the console:

Here is the sample output on my computer.

If the file you are trying to read is encoded using UTF-8, you can also read a the contents with a simple adjustment of passing the encoding. Here is an example code that reads the contents of a Groovy file as a UTF-8 string:

As you could see, reading contents is very concise in Groovy.

Read Contents line by line

Sometimes we don’t want to read the whole file contents into a single String. Sometimes we wish to read the contents line by line. Here is a sample code that reads the contents of a Groovy file line by line. Each line is shown on the screen with the corresponding line number.

Here is the sample output of the code above:

Read Contents To A List

If we want full control, we can read all the lines of a file as a list. Here is an example code that reads the contents of a Groovy file and assign the contents to a list.

Here is the sample output of the code above:

Write To File

There are multiple ways on how we can write to a file. Below is an example code that writes contents to a file using the write method and the left shift operator. Note that you need to add a line feed at the end of each line because both write and left shift don’t append a new line at the end.
Here is the sample output of the code fragment above.

Читайте также:  gigabyte ga x79 ud7

If we built the contents of what we wish to write to a String, we can write the contents in one go! We can assign the value to the text property of a file and it will automatically be written to the file.

This will be the contents of the file after running the code:

Test if File or Directory

Here is an example code to test if a Groovy file is a file or directory:

Here is a sample output:

Test if File is Hidden

Here is a sample code to test if a file is hidden:

Here is a sample output:

Create Directory

Creating a directory in Groovy is similar to Java. Here is an example code:

Copy File Contents

Here is an example code on how to read a text file and copy the contents to another file.

Delete File

Delete Multiple Files

Here is an example code that search for all files given a file name pattern, and then delete them one by one.

Rename a File

Listing Files

Here are some scenarios on how we can list files and directories in Groovy. Groovy file has convenience methods to help simplify code:

Listing Root File System
List Files and Subdirectories
List Subdirectories Only
List Files and Subdirectories Recursively

When a directory structure is deep, it is also possible to read it recursively without using recursion in your code. Just by using the function eachFileRecurse, the list of all files recursively will be returned. Here is an example code on how to list files recursively in Groovy.
Here is a sample output of the code above. Note that both files and subdirectories are listed recursively.

I created a small function that simply writes text to a file, but I am having issues making it write each piece of information to a new line. Can someone explain why it puts everything on the same line?

Here is my function:

The simple code I’m testing it with is something like this:

The above creates a text file in that folder and the contents of the text file look like this:

As you can see, the text is all bunched together instead of separated on a new line per text. I assume it has something to do with how I am adding it into my list?

Оцените статью
Добавить комментарий

Adblock detector