1 package com.arraySet;
2
3 import java.util.LinkedList;
4
5 public class Queue {
6 private LinkedList data = new LinkedList();
7
8 public Queue(){}
9
10 public boolean isEmpty(){
11 return data.isEmpty();
12 }
13
14 //往队列尾部添加对象
15 public void add(Object e){
16 this.data.addLast(e);
17 }
18 //查看队列首个对象
19 public Object peek(){
20 if(this.isEmpty()){
21 return null;
22 }
23 return this.data.getFirst();
24 }
25
26 //移除队列首个对象
27 public boolean remove(){
28 if(this.isEmpty()){
29 return false;
30 }
31 data.removeFirst();
32 return true;
33 }
34
35 //弹出首个对象
36 public Object pop(){
37 if(this.isEmpty()){
38 return null;
39 }
40 return data.removeFirst();
41 }
42
43 //查询任意对象在队列中的索引
44 public int index(Object e){
45 if(this.isEmpty()){
46 return -1;
47 }
48 return data.indexOf(e);
49 }
50
51 //清空队列
52 public void clear(){
53 data.clear();
54 }
55 }
1 package com.test;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import com.arraySet.Queue;
8
9 /**
10 * 查找给定的路径baseDirName下匹配符合targetFileName格式的count个文件
11 * @author Administrator
12 *
13 */
14 public class FileFinder {
15 public static List findfiles(String baseDirName,String targetFileName,int count){
16 List fileList = new ArrayList();
17 File baseDir = new File(baseDirName);
18
19 if(!baseDir.exists() || !baseDir.isDirectory()){
20 System.out.println("文件查找失败:"+baseDirName+"不存在或者不是目录!");
21 return fileList;
22 }
23 Queue queue = new Queue();
24 //用于临时保存队列文件夹中的文件名
25 String tempName = null;
26 queue.add(baseDir);
27 //外层循环
28 while(!queue.isEmpty()){
29 File tempFile = (File)queue.pop();
30 if(tempFile.exists() && tempFile.isDirectory()){
31 File[] files = tempFile.listFiles();
32 for(File file:files){
33 if(file.isDirectory()){
34 queue.add(file);
35 }else{
36 tempName = file.getName();
37 //判断文件名与表达式是否匹配
38 if(FileFinder.wildcardMatch(targetFileName, tempName)){
39 fileList.add(file.getAbsolutePath());
40 if((count!=0) && fileList.size()>=count){
41 //退出整个外部循环
42 return fileList;
43 }
44 }
45 }
46 }
47 }
48 }
49 return fileList;
50 }
51
52 /**
53 * 遗留问题:在类方法中的全局变量patternLength、strLength、tempLength,在嵌套循环中被调用,方法中的全局如何分别存储方法加载的值和嵌套循环的值
54 * @param pattern
55 * @param str
56 * @return
57 */
58 private static boolean wildcardMatch(String pattern, String str){
59 // System.out.print("pattern:"+pattern+"\t"+"str:"+str+"\t");
60 int patternLength = pattern.length();
61 int strLength = str.length();
62 int tempLength = 0;
63 char ch;
64 for(int patternIndex=0;patternIndex < patternLength;patternIndex++){
65 ch = pattern.charAt(patternIndex);
66 if(ch=='*'){
67 //*后面的字符串直到匹配到最后找到匹配为止,截取字符串从下标0开始,而字符串长度从1开始
68 while(tempLength < (strLength-1)){
69 // System.out.println("tempLength1:"+tempLength);
70 //调用的时候pattern、str值为截取后的字符串,调用结束返回时恢复为方法加载的全局值
71 if(wildcardMatch(pattern.substring(patternIndex + 1), str.substring(tempLength))){
72 return true;
73 }
74 tempLength++;
75 // System.out.println("tempLength2:"+tempLength);
76 }
77 }else if(ch=='?'){
78 tempLength++;
79 if(tempLength>strLength){
80 return false;
81 }
82 }else{
83 if((tempLength>strLength) || (ch!=str.charAt(tempLength))){
84 return false;
85 }
86 tempLength++;
87 }
88 }
89 return (tempLength == strLength);
90 }
91
92 private static void printArrayList(List list){
93 for(int i=0;i<list.size();i++){
94 System.out.println(list.get(i));
95 }
96 }
97 public static void main(String[] args) {
98 String baseDIR = "D:/software/jdk/jdk7/lib";
99 String targetFileName1 = "*.jar";
100 List fileList = FileFinder.findfiles(baseDIR, targetFileName1, 8);
101 FileFinder.printArrayList(fileList);
102 }
103 }
1 执行结果:
2 D:\software\jdk\jdk7\lib\ant-javafx.jar
3 D:\software\jdk\jdk7\lib\dt.jar
4 D:\software\jdk\jdk7\lib\javafx-doclet.jar
5 D:\software\jdk\jdk7\lib\javafx-mx.jar
6 D:\software\jdk\jdk7\lib\jconsole.jar
7 D:\software\jdk\jdk7\lib\sa-jdi.jar
8 D:\software\jdk\jdk7\lib\tools.jar
9 D:\software\jdk\jdk7\lib\missioncontrol\mc.jar
1 package com.test;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import com.arraySet.Queue;
8
9 /**
10 * 查找给定的路径baseDirName下匹配符合targetFileName格式的count个文件
11 * @author Administrator
12 *
13 */
14 public class FileFinder {
15 public static List findfiles(String baseDirName,String targetFileName,int count){
16 List fileList = new ArrayList();
17 File baseDir = new File(baseDirName);
18
19 if(!baseDir.exists() || !baseDir.isDirectory()){
20 System.out.println("文件查找失败:"+baseDirName+"不存在或者不是目录!");
21 return fileList;
22 }
23 Queue queue = new Queue();
24 //用于临时保存队列文件夹中的文件名
25 String tempName = null;
26 queue.add(baseDir);
27 //外层循环
28 while(!queue.isEmpty()){
29 File tempFile = (File)queue.pop();
30 if(tempFile.exists() && tempFile.isDirectory()){
31 File[] files = tempFile.listFiles();
32 for(File file:files){
33 if(file.isDirectory()){
34 queue.add(file);
35 }else{
36 tempName = file.getName();
37 //判断文件名与表达式是否匹配
38 if(FileFinder.wildcardMatch(targetFileName, tempName)){
39 fileList.add(file.getAbsolutePath());
40 if((count!=0) && fileList.size()>=count){
41 //退出整个外部循环
42 return fileList;
43 }
44 }
45 }
46 }
47 }
48 }
49 return fileList;
50 }
51
52 /**
53 * 遗留问题:在类方法中的全局变量patternLength、strLength、tempLength,在嵌套循环中被调用,
54 * 方法中的全局如何分别存储方法加载的值和嵌套循环的值
55 *
56 * private方法不提供给外部类调用,仅提供类内部方法调用,由于是static状态的,所以提供给内部static状态的方法调用
57 * private方法不能调用任何方法
58 * @param pattern
59 * @param str
60 * @return
61 */
62 private static boolean wildcardMatch(String pattern, String str){
63
64 int patternLength = pattern.length();
65 int strLength = str.length();
66 int tempLength = 0;
67 char ch;
68 // System.out.print("patternLength:"+patternLength+"\t"+"strLength:"+strLength+"\t");
69 for(int patternIndex=0;patternIndex < patternLength;patternIndex++){
70 ch = pattern.charAt(patternIndex);
71 if(ch=='*'){
72 //*后面的字符串直到匹配到最后找到匹配为止,截取字符串从下标0开始,而字符串长度从1开始
73 while(tempLength < (strLength-1)){
74 // System.out.println("tempLength1:"+tempLength);
75 // System.out.print(wildcardMatch(pattern.substring(patternIndex + 1), str.substring(tempLength))+"\t");
76 //调用的时候pattern、str值为截取后的字符串,调用结束返回时恢复为方法加载的全局值
77 if(wildcardMatch(pattern.substring(patternIndex + 1), str.substring(tempLength))){
78
79 return true;
80 }
81 tempLength++;
82 // System.out.println("tempLength2:"+tempLength);
83 }
84 }else if(ch=='?'){
85 tempLength++;
86 if(tempLength>strLength){
87 return false;
88 }
89 }else{
90 if((tempLength>strLength) || (ch!=str.charAt(tempLength))){
91 return false;
92 }
93 tempLength++;
94 }
95 }
96 return (tempLength == strLength);
97 }
98
99 protected static void printArrayList(List list){
100 if(list.size()==0){
101 System.out.println("No file find!");
102 }else{
103 for(int i=0;i<list.size();i++){
104 System.out.println(list.get(i));
105 }
106 }
107 }
108
109 public static void main(String[] args) {
110 String baseDIR = "D:/software/jdk/jdk7/lib";
111 // String baseDIR = "D:/testfile";
112 // String targetFileName1 = "*.jar";
113 // String targetFileName1 = "?.jar";
114 // String targetFileName1 = "a*.jar";
115 String targetFileName1 = "*l*.jar";
116 //因为FileFinder.findfiles(...)返回的是一个static的变量,所以引用static的方法也需要是static状态的
117 List fileList = FileFinder.findfiles(baseDIR, targetFileName1, 8);
118 FileFinder.printArrayList(fileList);
119
120 }
121
122
123 }
1 执行结果:
2 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
3 at java.lang.String.charAt(String.java:658)
4 at com.test.FileFinder.wildcardMatch(FileFinder.java:90)
5 at com.test.FileFinder.findfiles(FileFinder.java:38)
6 at com.test.FileFinder.main(FileFinder.java:117)
1 package com.test;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import com.arraySet.Queue;
8
9 /**
10 * 查找给定的路径baseDirName下匹配符合targetFileName格式的count个文件
11 * @author Administrator
12 *
13 */
14 public class FileFinder {
15 public static List findfiles(String baseDirName,String targetFileName,int count){
16 List fileList = new ArrayList();
17 File baseDir = new File(baseDirName);
18
19 if(!baseDir.exists() || !baseDir.isDirectory()){
20 System.out.println("文件查找失败:"+baseDirName+"不存在或者不是目录!");
21 return fileList;
22 }
23 Queue queue = new Queue();
24 //用于临时保存队列文件夹中的文件名
25 String tempName = null;
26 queue.add(baseDir);
27 //外层循环
28 while(!queue.isEmpty()){
29 File tempFile = (File)queue.pop();
30 if(tempFile.exists() && tempFile.isDirectory()){
31 File[] files = tempFile.listFiles();
32 for(File file:files){
33 if(file.isDirectory()){
34 queue.add(file);
35 }else{
36 tempName = file.getName();
37 //判断文件名与表达式是否匹配
38 if(FileFinder.wildcardMatch(targetFileName, tempName)){
39 fileList.add(file.getAbsolutePath());
40 if((count!=0) && fileList.size()>=count){
41 //退出整个外部循环
42 return fileList;
43 }
44 }
45 }
46 }
47 }
48 }
49 return fileList;
50 }
51
52 /**
53 * 遗留问题:在类方法中的全局变量patternLength、strLength、tempLength,在嵌套循环中被调用,
54 * 方法中的全局如何分别存储方法加载的值和嵌套循环的值
55 *
56 * private方法不提供给外部类调用,仅提供类内部方法调用,由于是static状态的,所以提供给内部static状态的方法调用
57 * private方法不能调用任何方法
58 * @param pattern
59 * @param str
60 * @return
61 */
62 private static boolean wildcardMatch(String pattern, String str){
63
64 int patternLength = pattern.length();
65 int strLength = str.length();
66 int tempLength = 0;
67 char ch;
68 // System.out.print("patternLength:"+patternLength+"\t"+"strLength:"+strLength+"\t");
69 for(int patternIndex=0;patternIndex < patternLength;patternIndex++){
70 ch = pattern.charAt(patternIndex);
71 if(ch=='*'){
72 //*后面的字符串直到匹配到最后找到匹配为止,截取字符串从下标0开始,而字符串长度从1开始
73 while(tempLength < (strLength-1)){
74 // System.out.println("tempLength1:"+tempLength);
75 // System.out.print(wildcardMatch(pattern.substring(patternIndex + 1), str.substring(tempLength))+"\t");
76 //调用的时候pattern、str值为截取后的字符串,调用结束返回时恢复为方法加载的全局值
77 if(wildcardMatch(pattern.substring(patternIndex + 1), str.substring(tempLength))){
78 return true;
79 }
80 tempLength++;
81 // System.out.println("tempLength2:"+tempLength);
82 }
83 }else if(ch=='?'){
84 tempLength++;
85 if(tempLength > (strLength-1)){
86 return false;
87 }
88 }else{
89 if((tempLength > (strLength-1)) || (ch!=str.charAt(tempLength))){
90 return false;
91 }
92 tempLength++;
93 }
94 }
95 return (tempLength == strLength);
96 }
97
98 protected static void printArrayList(List list){
99 if(list.size()==0){
100 System.out.println("No file find!");
101 }else{
102 for(int i=0;i<list.size();i++){
103 System.out.println(list.get(i));
104 }
105 }
106 }
107
108 public static void main(String[] args) {
109 String baseDIR = "D:/software/jdk/jdk7/lib";
110 // String baseDIR = "D:/testfile";
111 // String targetFileName1 = "*.jar";
112 // String targetFileName1 = "?.jar";
113 // String targetFileName1 = "a*.jar";
114 // String targetFileName1 = "*a?.jar";
115 // String targetFileName1 = "?a*.jar";
116 // String targetFileName1 = "?*a.jar";
117 // String targetFileName1 = "a?*.jar";
118 String targetFileName1 = "*b*.jar";
119 //因为FileFinder.findfiles(...)返回的是一个static的变量,所以引用static的方法也需要是static状态的
120 List fileList = FileFinder.findfiles(baseDIR, targetFileName1, 8);
121 FileFinder.printArrayList(fileList);
122
123 }
124
125
126 }
1 D:\software\jdk\jdk7\lib\missioncontrol\plugins\com.ibm.icu_52.1.0.v201404241930.jar
2 D:\software\jdk\jdk7\lib\missioncontrol\plugins\com.jrockit.mc.browser.attach.ja_5.5.0.165303.jar
3 D:\software\jdk\jdk7\lib\missioncontrol\plugins\com.jrockit.mc.browser.attach.zh_CN_5.5.0.165303.jar
4 D:\software\jdk\jdk7\lib\missioncontrol\plugins\com.jrockit.mc.browser.attach_5.5.0.165303.jar
5 D:\software\jdk\jdk7\lib\missioncontrol\plugins\com.jrockit.mc.browser.ja_5.5.0.165303.jar
6 D:\software\jdk\jdk7\lib\missioncontrol\plugins\com.jrockit.mc.browser.jdp.ja_5.5.0.165303.jar
7 D:\software\jdk\jdk7\lib\missioncontrol\plugins\com.jrockit.mc.browser.jdp.zh_CN_5.5.0.165303.jar
8 D:\software\jdk\jdk7\lib\missioncontrol\plugins\com.jrockit.mc.browser.jdp_5.5.0.165303.jar