-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirList.java
More file actions
46 lines (40 loc) · 1.13 KB
/
Copy pathDirList.java
File metadata and controls
46 lines (40 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.example;
import java.io.*;
import java.util.Arrays;
import java.util.regex.Pattern;
public class DirList {
/**
* @param args
*/
public static void main(final String[] args) {
// TODO Auto-generated method stub
File path = new File(".");
String[] list;
// if(args.length == 0)
// list = path.list();
// else
// list = path.list(new DirFilter(args[0]));
// Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
//使用匿名内部类实现 args要加上final
if(args.length == 0)
list = path.list();
else
list = path.list(new FilenameFilter(){
private Pattern pattern=Pattern.compile(args[0]);
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
});
for(String dirItem : list)
System.out.println(dirItem);
}
}
class DirFilter implements FilenameFilter {
private Pattern pattern;
public DirFilter(String regex) {
pattern = Pattern.compile(regex);
}
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
}