Groovy_ traversing files

Source: Internet
Author: User
Tags closure soapui

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-smo Ke-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.lan G.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.na Me) 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 dir Ectory.

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 thi S 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
        }
    } 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.