zno2

查找某文件夹下所有java文件(包括子文件下的文件)

 

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.0</version>
        </dependency>

 

 

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import org.apache.commons.io.FileUtils;

public class TTTT {

    public static void main(String args1[]) throws IOException {
        File dir = new File("E:/test");
        
        // 这个列出test文件夹下所有的java文件,包括subdirectories 的
        System.out.println("Start1:\n");
        if (dir.exists()) {
            Collection<File> files = FileUtils.listFiles(dir, new String[] { "java" }, true);
            for (File file : files) {
                String canonicalPath = file.getCanonicalPath();
                System.out.println(canonicalPath);
            }
        }
        // 这个只列出test文件夹下的文件
        System.out.println("Start2:\n");
        if (dir.exists()) {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.getName().endsWith(".java")) {
                    String canonicalPath = file.getCanonicalPath();
                    System.out.println(canonicalPath);
                }
            }
        }
    }

}

 

Open Declaration Collection<File> org.apache.commons.io.FileUtils.listFiles(File directory, String[] extensions, boolean recursive)


Finds files within a given directory (and optionally its subdirectories) which match an array of extensions.

Parameters:
directory the directory to search in
extensions an array of extensions, ex. {"java","xml"}. If this parameter is null, all files are returned.
recursive if true all subdirectories are searched as well
Returns:
an collection of java.io.File with the matching files
FileUtils.listFiles(directory,extensions,recursive);
Open Declaration File[] java.io.File.listFiles()


Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. 

If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory. Pathnames denoting the directory itself and the directory's parent directory are not included in the result. Each resulting abstract pathname is constructed from this abstract pathname using the File(File, String) constructor. Therefore if this pathname is absolute then each resulting pathname is absolute; if this pathname is relative then each resulting pathname will be relative to the same directory. 

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Returns:
An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
Throws:
SecurityException - If a security manager exists and its java.lang.SecurityManager.checkRead(java.lang.String) method denies read access to the directory
Since:
1.2

file.listFiles();

 

posted on 2023-06-01 17:06  zno2  阅读(82)  评论(0编辑  收藏  举报

导航