
- Groovy Tutorial
- Groovy - Home
- Groovy - Overview
- Groovy - Environment
- Groovy - Basic Syntax
- Groovy - Data Types
- Groovy - Variables
- Groovy - Optionals
- Groovy - Numbers
- Groovy - Strings
- Groovy - Ranges
- Groovy - Lists
- Groovy - Maps
- Groovy - Dates & Times
Groovy Operators
- Groovy - Operators
- Groovy - Arithmetic Operators
- Groovy - Assignment Operators
- Groovy - Relational Operators
- Groovy - Logical Operators
- Groovy - Bitwise Operators
- Groovy - Spaceship Operator
- Groovy - in Operator
- Groovy - Elvis Operator
- Groovy - Safe Navigation Operator
- Groovy Operator Precedence & Associativity
Control Statements
- Groovy - Decision Making
- Groovy - If Else Statement
- Groovy - Switch Statement
- Groovy - Loops
- Groovy - For Loop
- Groovy - For-in Loop
- Groovy - While Loop
- Groovy - Do While Loop
- Groovy - Break Statement
- Groovy - Continue Statement
Groovy File Handling
- Groovy - File I/O
- Java - Create a File
- Java - Write to File
- Java - Append to File
- Java - Read Files
- Java - Delete Files
- Java - File Properties
- Java - File Existence and Type
- Java - File Size
- Java - File Permissions
- Java - Directories
- Java - Listing Directories
- Java - Filtering Files/Directories
- Java - Deleting Directories
- Java - Renaming Files/Directories
Groovy Error & Exceptions
- Groovy - Exception Handling
- Groovy - try-catch Block
- Groovy - try-with-resources
- Groovy - Multi-catch Block
- Groovy - Nested try Block
- Groovy - Finally Block
- Groovy - throw Exception
- Groovy - Exception Propagation
- Groovy - Built-in Exceptions
- Groovy - Custom Exception
Groovy Multithreading
- groovy - Multithreading
- groovy - Thread Life Cycle
- groovy - Creating a Thread
- groovy - Starting a Thread
- groovy - Joining Threads
- groovy - Naming Thread
- groovy - Thread Scheduler
- groovy - Thread Pools
- groovy - Main Thread
- groovy - Thread Priority
- groovy - Daemon Threads
- groovy - Shutdown Hook
Groovy Synchronization
- groovy - Synchronization
- groovy - Block Synchronization
- groovy - Static Synchronization
- groovy - Inter-thread Communication
- groovy - Thread Deadlock
- groovy - Interrupting a Thread
- groovy - Thread Control
- groovy - Reentrant Monitor
- Groovy - Methods
- Groovy - Methods
- Groovy - Optional parenthesis
- Groovy - Named Arguments
- Groovy - Closures as Arguments
- Groovy - Method Overloading
- Groovy - Method Scope and Visibility
- Groovy - isCase Method
- Groovy - Implicit Return
- Groovy - Variable Arguments
- Groovy - Regular Expressions
- Groovy - Regular Expressions
- Groovy - Defining Regular Expressions
- Groovy - Matcher Object
- Groovy - Regex Tasks
- Groovy - XML
- Groovy - XML
- Groovy - Parsing XML
- Groovy - Creating XML
- Groovy - Modifying XML
- Groovy - Querying XML
- Groovy - Simplified Notation
- Groovy - Closure based Querying
- Groovy - Closure based Creation
- Groovy - JSON
- Groovy - JSON
- Groovy - Parsing JSON
- Groovy - Creating JSON using JsonOutput
- Groovy - Creating JSON using JsonBuilder
- Groovy - Modifying JSON
- Groovy - Error Handling
- Groovy - Handling JSON Arrays
- Groovy - JSON Array Operations
- Groovy - JSON Objects
- Groovy - JSON Object Operations
- Groovy - Generics
- Groovy - Generics
- Groovy - Declaring Generic Types
- Groovy - Bound Type Parameters
- Groovy - Wild Cards
- Groovy - Miscellaneous
- Groovy - Object Oriented
- Groovy - Closures
- Groovy - Annotations
- Groovy - JMX
- Groovy - DSLS
- Groovy - Database
- Groovy - Builders
- Groovy - Command Line
- Groovy - Unit Testing
- Groovy - Template Engines
- Groovy - Meta Object Programming
- Groovy Useful Resources
- Groovy - Quick Guide
- Groovy - Useful Resources
- Groovy - Discussion
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