Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Listing Directories



Groovy provides File class which represents a directory object and provides couple of options to list contents of a directory using File object.

Example - Getting names of files/directories in directory

File.list() method returns an array of String objects where each string represent the name of the file or directory in the directory represented by the File object.

Example.groovy

// get the current directory
def currentDir = new File('.')
 
// get names of all files/directories within current directory
def contents = currentDir.list()

println "Listing of '${currentDir.name}':"
contents.each { println it }

Output

When we run the above program, we will get the following result.

Listing of '.':
data.txt
Example.groovy
log.txt
Test Directory
test.txt
test1.txt

Example - Getting File Objects of files/directories in directory

File.listFiles() method returns an array of File objects where each file object represent file or directory in the directory represented by the File object. It is particulary useful when we need to inspect further properties.

Example.groovy

// get the current directory
def currentDir = new File('.')
 
// get file objects for files and directories within current directory
def files = currentDir.listFiles()

println "Files in '${currentDir.name}':"
files.each { println "${it.name} (isDir: ${it.isDirectory()}, isFile: ${it.isFile()})" }

Output

When we run the above program, we will get the following result.

Files in '.':
data.txt (isDir: false, isFile: true)
Example.groovy (isDir: false, isFile: true)
log.txt (isDir: false, isFile: true)
Test Directory (isDir: true, isFile: false)
test.txt (isDir: false, isFile: true)
test1.txt (isDir: false, isFile: true)

Example - Listing files/directories with Closure

File.eachFile(Closure closure) method allows iteration on File objects lying in current file object while passing a closure having a file object as parameter.

Example.groovy

new File('.').eachFile { file ->
   println "${file.name} is a ${file.isDirectory() ? 'Directory' : 'File'}."
}

Output

When we run the above program, we will get the following result.

data.txt is a File.
Example.groovy is a File.
log.txt is a File.
Test Directory is a Directory.
test.txt is a File.
test1.txt is a File.

Example - Listing files/directories Recursively with Closure

File.eachFileRecursive(Closure closure) method allows iteration on File objects lying in current file object as well as in subdirectories if file reprsents a directory while passing a closure having a file object as parameter.

Example.groovy

new File('.').eachFileRecurse { file ->
   println "File Name: ${file.absolutePath}"
}

Output

When we run the above program, we will get the following result.

File Name: E:\Dev\groovy\.\data.txt
File Name: E:\Dev\groovy\.\Example.groovy
File Name: E:\Dev\groovy\.\log.txt
File Name: E:\Dev\groovy\.\Test Directory
File Name: E:\Dev\groovy\.\Test Directory\Test 1
File Name: E:\Dev\groovy\.\Test Directory\Test 1\sample.txt
File Name: E:\Dev\groovy\.\test.txt
File Name: E:\Dev\groovy\.\test1.txt
Advertisements