Groovy_遍历文件目录

本文介绍了Groovy中遍历文件目录的方法,包括void eachDir(Closure closure)用于遍历子目录忽略普通文件,void eachDirMatch(Object nameFilter, Closure closure)用于按名称过滤子目录,以及eachDirRecurse(File self, Closure closure)用于递归遍历所有子孙目录。通过这些方法,可以方便地操作和管理文件系统中的目录结构。" 128362609,5679753,C#实现Word到RTF转换教程,"['C#开发', '编程语言', '文档转换', '游戏开发', '资源分享']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Official documentation: http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/File.html

Common method:

1. void eachDir(Closure closure): Invokes the closure for each subdirectory in this directory, ignoring regular files.

class eachDirTest {
        public static void main(String[] args) {
            def count = 0
            def dir = new File("D:\\Project\\SoapUI\\Project-smoke-tests")

            dir.eachDir{directory->
                println directory
            }
        }  
} 

2. void eachDirMatch(Object nameFilter, Closure closure): Invokes the closure for each subdirectory whose name (dir.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods#isCase(java.lang.Object, method to determine if a match occurs.

class eachDirTest {
    public static void main(String[] args) {
        def count = 0
        def dir = new File("D:\\Project\\SoapUI\\Project-smoke-tests")

        dir.eachDirMatch(~/^Sanity.*/){d ->
            println d
        }
    } 
}

Output:
D:\Project\SoapUI\Project-smoke-tests\SanityTestSuite1
D:\Project\SoapUI\Project-smoke-tests\SanityTestSuite2
D:\Project\SoapUI\Project-smoke-tests\SanityTestSuite3
D:\Project\SoapUI\Project-smoke-tests\SanityTestSuite4
D:\Project\SoapUI\Project-smoke-tests\SanityTestSuite5
……..

3. eachDirRecurse(File self, Closure closure) : Invokes the closure for each descendant directory of this directory.

class eachDirTest {
        public static void main(String[] args) {
            def count = 0
            def dir = new File("D:\\Project\\SoapUI\\Project-smoke-tests")

            dir.eachDirRecurse{directory->
                println directory
            }
        }  
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值