1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package com.sysmodel.configLoader;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class FileUtil{
- public static File[] getFiles(String path) {
- if (path.indexOf("\\") == -1) {
- return null;
- }
- String pattern = path.substring(path.lastIndexOf("\\") + 1);
- String dir = path.substring(0, path.lastIndexOf("\\") + 1);
- return getFiles(dir, pattern);
- }
- public static File[] getFiles(String dir, String pattern) {
- File file = new File(dir);
- pattern = pattern.replace(',', '#');
- pattern = pattern.replaceAll("#", "\\\\.");
- pattern = pattern.replace('*', '#');
- pattern = pattern.replaceAll("#", ".*");
- pattern = pattern.replace('?', '#');
- pattern = pattern.replaceAll("#", ".?");
- pattern = "^" + pattern + "$";
- Pattern p = Pattern.compile(pattern);
- List<File> list = filePattern(file, p);
- File[] files = new File[list.size()];
- list.toArray(files);
- return files;
- }
- private static List<File> filePattern(File file, Pattern p) {
- if (null == file)
- return null;
- else if (file.isFile()) {
- Matcher matcher = p.matcher(file.getName());
- if (matcher.matches()) {
- List<File> list = new ArrayList<File>();
- list.add(file);
- return list;
- }
- } else if (file.isDirectory()) {
- File[] files = file.listFiles();
- if (null != files && files.length > 0) {
- List<File> list = new ArrayList<File>();
- for (File f : files) {
- List<File> rlist = filePattern(f, p);
- if (null != rlist) {
- list.addAll(rlist);
- }
- }
- return list;
- }
- }
- return null;
- }
- public static void main(String[] args) {
- // File[] files =
- // FileUtil.getFiles("E:\\workspace\\bjgls\\build\\classes",
- // "applicationContext*.xml");
- FileUtil.getFiles("/WEB-INF/DataModel*.xml");
- }
- }
|