Java tutorial
//package com.java2s; /* * Copyright 2013 Google Inc. All rights reserved. * * 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.FilenameFilter; import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /** * Obtains a list of files that live in the specified directory and match the glob pattern. */ public static String[] getFiles(File dir, String glob) { String regex = globToRegex(glob); final Pattern pattern = Pattern.compile(regex); String[] result = dir.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { Matcher matcher = pattern.matcher(name); return matcher.matches(); } }); Arrays.sort(result); return result; } /** * Converts a filename globbing pattern to a regular expression. * <p> * The regex is suitable for use by Matcher.matches(), which matches the entire string, so * we don't specify leading '^' or trailing '$'. */ private static String globToRegex(String glob) { // Quick, overly-simplistic implementation -- just want to handle something simple // like "*.mp4". // // See e.g. http://stackoverflow.com/questions/1247772/ for a more thorough treatment. StringBuilder regex = new StringBuilder(glob.length()); //regex.append('^'); for (char ch : glob.toCharArray()) { switch (ch) { case '*': regex.append(".*"); break; case '?': regex.append('.'); break; case '.': regex.append("\\."); break; default: regex.append(ch); break; } } //regex.append('$'); return regex.toString(); } }