Here you can find the source of getFilesInFolderByRegex(File folder, final String regex)
Parameter | Description |
---|---|
FileNotFoundException | an exception |
private static File[] getFilesInFolderByRegex(File folder, final String regex) throws FileNotFoundException
//package com.java2s; /*//w w w. j av a2 s. c o m * Copyright 2012 LinkedIn Corp. * * Licensed 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.FileNotFoundException; import java.io.FilenameFilter; public class Main { /** * * @return a list of files in the given folder that matches the regex. It may be empty, but will * never return a null * @throws FileNotFoundException */ private static File[] getFilesInFolderByRegex(File folder, final String regex) throws FileNotFoundException { // sanity check if (!folder.exists()) { throw new FileNotFoundException(); } if (!folder.isDirectory()) { throw new IllegalStateException( "execution jar is suppose to be in this folder, but the object present is not a directory: " + folder); } File[] matchingFiles = folder.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { if (name.matches(regex)) return true; else return false; } }); if (matchingFiles == null) { throw new IllegalStateException( "the File[] matchingFiles variable is null. This means an IOException occured while doing listFiles. Please check disk availability and retry again"); } return matchingFiles; } }