Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Groovy file I/O


May 14, 2021 Groovy


Table of contents


Groovy provides many aids when using I/O, and Groovy provides simpler classes to provide the following functionality for files.

  • Read the file
  • Write to the file
  • Traverse the file tree
  • Read and write data objects to files

In addition, you can always use the standard Java classes listed below for file I/O operations.

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

Read the file

The following example outputs all lines of a text file in Groovy. /b10> Methodeline is built into the File class in Groovy to ensure that every line of the text file is read.

import java.io.File 
class Example { 
   static void main(String[] args) { 
      new File("E:/Example.txt").eachLine {  
         line -> println "line : $line"; 
      } 
   } 
}

The File class is used to instantiate new objects with file names as parameters. /b10> It then accepts the eachLine function, places it in a line variable, and prints it accordingly.

If the files contain the following lines, they will be printed.

line : Example1
line : Example2

Read the contents of the file to the 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. /b10> The following example shows how to do this.

class Example { 
   static void main(String[] args) { 
      File file = new File("E:/Example.txt") 
      println file.text 
   } 
}

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

line : Example1 
line : Example2

Write to the file

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

import java.io.File 
class Example { 
   static void main(String[] args) { 
      new File('E:/','Example.txt').withWriter('utf-8') { 
         writer -> writer.writeLine 'Hello World' 
      }  
   } 
}

If you open the file example .txt, you'll see the word "Hello World" printed in the text.

Gets the size of the file

If you want to get the size of a file, you can use the legth property of the file class to get it, and the following example shows how to do this.

class Example {
   static void main(String[] args) {
      File file = new File("E:/Example.txt")
      println "The file ${file.absolutePath} has ${file.length()} bytes"
   } 
}

The code above shows the size of the file, in bytes.

Test whether the file is a directory

If you want to see if the path is a file or a directory, you can use the isFile and isDirectory options for the File class. The following example shows how to do this.

class Example { 
   static void main(String[] args) { 
      def file = new File('E:/') 
      println "File? ${file.isFile()}" 
      println "Directory? ${file.isDirectory()}" 
   } 
}

The above code will show the following output -

File? false 
Directory? True

Create 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 to do this.

class Example {
   static void main(String[] args) {
      def file = new File('E:/Directory')
      file.mkdir()
   } 
}

If directory E:\Directory does not exist, it will be created.

Delete the file

If you want to delete a file, you can use the delete feature of the File class. The following example shows how to do this.

class Example {
   static void main(String[] args) {
      def file = new File('E:/Example.txt')
      file.delete()
   } 
}

If the file exists, it will be deleted.

Copy the file

Groovy also provides the ability to copy content from one file to another. The following example shows how to do this.

class Example {
   static void main(String[] args) {
      def src = new File("E:/Example.txt")
      def dst = new File("E:/Example1.txt")
      dst << src.text
   } 
}

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

Get the contents of the catalog

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

The following example shows how to use the ListRoots function of the File class to display drives on the machine.

class Example { 
   static void main(String[] args) { 
      def rootFiles = new File("test").listRoots() 
      rootFiles.each { 
         file -> println file.absolutePath 
      }
   }
}

The output may vary depending on the drive available on the machine. The output on the standard machine will be similar to one of the following -

C:\
D:\

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

class Example {
   static void main(String[] args) {
      new File("E:/Temp").eachFile() {  
         file->println file.getAbsolutePath()
      }
   } 
}

The output will show all the files in directory E: . . . Temp

If you want to recursively display all the files in the directory and its subdirectors, you can use the eachFileRecurse function of the File class. The following example shows how to do this.

class Example { 
   static void main(String[] args) {
      new File("E:/temp").eachFileRecurse() {
         file -> println file.getAbsolutePath()
      }
   }
} 

The output displays all the files in directory E: , Temp and their subdirectts, if any.