Groovy_遍历文件

本文介绍了Groovy中遍历文件的方法,包括`eachFile`、`eachFileMatch`和`eachFileRecurse`等,详细阐述了它们的参数和使用场景,如根据文件类型过滤、递归遍历子目录等。

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

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

Common Method:

1. eachFile(File self, Closure closure):Invokes the closure for each ‘child’ file or directory in this ‘parent’ folder/directory.

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

        dir.eachFile{file ->
            println file
        }
    } 
}

2. eachFile(File self, FileType fileType, Closure closure): Invokes the closure for each ‘child’ file or directory in this ‘parent’ folder/directory.

Param FileType is an Enum:
ANY: Represents both normal files and directories
DIRECTORIES: Represents directories
FILES: Represents normal files

import groovy.io.FileType

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

        //Only output the normal files
        dir.eachFile(FileType.FILES){file ->
            println file
        }
    } 
}

3. eachFileMatch(File self, FileType fileType, Object nameFilter, Closure closure): Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling the isCase(java.lang.Object, java.lang.Object) method to determine if a match occurs.

import groovy.io.FileType

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

        //Only output the normal files
        dir.eachFileMatch(FileType.FILES, ~/^Data.*/){file ->
            println file
        }
    } 
}

4. eachFileMatch(File self, Object nameFilter, Closure closure): Invokes the closure for each file whose name (file.name) matches the given nameFilter in the given directory - calling the isCase(java.lang.Object, java.lang.Object) method to determine if a match occurs.

import groovy.io.FileType

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

        dir.eachFileMatch(~/^Sanity.*/){file ->
            println file
        }
    } 
}

5. eachFileRecurse(File self, Closure closure): Invokes the closure for each descendant file and directory in this directory.

import groovy.io.FileType

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

        dir.eachFileRecurse{file ->
            println file
        }
    } 
}

6. eachFileRecurse(File self, FileType fileType, Closure closure): Invokes the closure for each descendant file in this directory.

import groovy.io.FileType

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

        //Only output the normal files
        dir.eachFileRecurse(FileType.FILES){file ->
            println file
        }
    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值