81b5f229fa153292aa5ec1e13a0fd25243448655.svn-base 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.sysmodel.configLoader;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. public class FileUtil{
  8. public static File[] getFiles(String path) {
  9. if (path.indexOf("\\") == -1) {
  10. return null;
  11. }
  12. String pattern = path.substring(path.lastIndexOf("\\") + 1);
  13. String dir = path.substring(0, path.lastIndexOf("\\") + 1);
  14. return getFiles(dir, pattern);
  15. }
  16. public static File[] getFiles(String dir, String pattern) {
  17. File file = new File(dir);
  18. pattern = pattern.replace(',', '#');
  19. pattern = pattern.replaceAll("#", "\\\\.");
  20. pattern = pattern.replace('*', '#');
  21. pattern = pattern.replaceAll("#", ".*");
  22. pattern = pattern.replace('?', '#');
  23. pattern = pattern.replaceAll("#", ".?");
  24. pattern = "^" + pattern + "$";
  25. Pattern p = Pattern.compile(pattern);
  26. List<File> list = filePattern(file, p);
  27. File[] files = new File[list.size()];
  28. list.toArray(files);
  29. return files;
  30. }
  31. private static List<File> filePattern(File file, Pattern p) {
  32. if (null == file)
  33. return null;
  34. else if (file.isFile()) {
  35. Matcher matcher = p.matcher(file.getName());
  36. if (matcher.matches()) {
  37. List<File> list = new ArrayList<File>();
  38. list.add(file);
  39. return list;
  40. }
  41. } else if (file.isDirectory()) {
  42. File[] files = file.listFiles();
  43. if (null != files && files.length > 0) {
  44. List<File> list = new ArrayList<File>();
  45. for (File f : files) {
  46. List<File> rlist = filePattern(f, p);
  47. if (null != rlist) {
  48. list.addAll(rlist);
  49. }
  50. }
  51. return list;
  52. }
  53. }
  54. return null;
  55. }
  56. public static void main(String[] args) {
  57. // File[] files =
  58. // FileUtil.getFiles("E:\\workspace\\bjgls\\build\\classes",
  59. // "applicationContext*.xml");
  60. FileUtil.getFiles("/WEB-INF/DataModel*.xml");
  61. }
  62. }