Here you can find the source of getAllFilesMatching(File srcDir, final String regex)
Parameter | Description |
---|---|
srcDir | a parameter |
regex | a parameter |
public static List<File> getAllFilesMatching(File srcDir, final String regex)
//package com.java2s; /* /* w ww. j a v a2 s .c om*/ * Licensed to the soi-toolkit project under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The soi-toolkit project licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { /** * * @param srcDir * @param regex * @return */ public static List<File> getAllFilesMatching(File srcDir, final String regex) { List<File> foundFiles = new ArrayList<File>(); if (!srcDir.isDirectory()) { if (srcDir.getPath().matches(regex)) { foundFiles.add(srcDir); return foundFiles; } return foundFiles; } FilenameFilter filenameFiler = new FilenameFilter() { public boolean accept(File dir, String name) { return name.matches(regex); }; }; foundFiles.addAll(Arrays.asList(srcDir.listFiles(filenameFiler))); FileFilter fileFiler = new FileFilter() { public boolean accept(File file) { return file.isDirectory(); }; }; File[] directories = srcDir.listFiles(fileFiler); for (File file : directories) { foundFiles.addAll(getAllFilesMatching(file, regex)); } return foundFiles; } }