Groovy_遍历文件目录

来源:互联网 发布:手机照片分类软件 编辑:程序博客网 时间:2024/04/28 01:37

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            }        }  } 
0 0
原创粉丝点击