我是靠谱客的博主 愤怒洋葱,这篇文章主要介绍Groovy_遍历文件,现在分享给大家,希望可以做个参考。

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.

复制代码
1
2
3
4
5
6
7
8
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

复制代码
1
2
3
4
5
6
7
8
9
10
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.

复制代码
1
2
3
4
5
6
7
8
9
10
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.

复制代码
1
2
3
4
5
6
7
8
9
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.

复制代码
1
2
3
4
5
6
7
8
9
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.

复制代码
1
2
3
4
5
6
7
8
9
10
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 } } }

最后

以上就是愤怒洋葱最近收集整理的关于Groovy_遍历文件的全部内容,更多相关Groovy_遍历文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部