to binriyue:
不是,我是想在无界面的程序中使用上述功能,不过用java的File类来实现很麻烦(比如windows下的文件名是不区分大小写的,而java的排序功能是分大小写的;按最后修改时间排序要取每个文件的最后修改时间,然后再排序;支持通配符要自己实现FilenameFilter接口),我想知道java中有没有现成的类,有没有简单点的方法。
public class FindWildcardFiles {
public FindWildcardFiles(){
File f = new File( "C:/My Documents/My Pictures" );
theFilter filter= new theFilter("*gi*i*.jpg");//也可为,*gi*但是对gi*的形式就不行了,再想想........
File[] files = f.listFiles(filter);
for ( int i=0; i<files.length-1; i++ ){
System.out.println(files[i]);
}
}
public static void main( String[] args ){
new FindWildcardFiles();
}
}
class theFilter implements FileFilter{
private char[] pattern;
public theFilter(String s){
s = s.trim();
s = toMinimalPattern(s);
s = s.toUpperCase();
pattern = s.toCharArray();
}
public boolean accept(File pathname){
String s = pathname.getPath();
s = s.toUpperCase();
char[] ca = s.toCharArray();
return match(pattern,0,ca,0);
}
public static String toMinimalPattern(String s){
int len = s.length();
StringBuffer sb = new StringBuffer();
int x = 0;
int y = s.indexOf('*');
while (x <= y){
y++;
sb.append(s.substring(x,y));
x = y;
while (y < len && s.charAt(y) == '*'){
y++;
x = y;
}
y = s.indexOf('*',x);
}
sb.append(s.substring(x));
return sb.toString();
}
/**
* An implementation of the FileFilter interfaces that filters files based
* on a DOS file name pattern. DOS file name patterns are case-insensitive
* and make use of the ? and * wildcard characters.
*/
public class MYFileFilter implements FileFilter{
private char[] pattern;
/**
* Creates a new DOSFileFilter with the provided pattern. The
* toMinimalPattern() method is implicitly called on the provided
* pattern.
*/
public MYFileFilter(String s){
s = s.trim();
s = toMinimalPattern(s);
s = s.toUpperCase();
pattern = s.toCharArray();
}
public boolean accept(File pathname){
String s = pathname.getPath();
s = s.toUpperCase();
char[] ca = s.toCharArray();
return match(pattern,0,ca,0);
}
/**
* Converts the provided pattern to it's minimum equivalent. Basically
* this method converts all touching *'s to a single *.
*/
public static String toMinimalPattern(String s){
int len = s.length();
StringBuffer sb = new StringBuffer();
int x = 0;
int y = s.indexOf('*');
while (x <= y){
y++;
sb.append(s.substring(x,y));
x = y;
while (y < len && s.charAt(y) == '*'){
y++;
x = y;
}
y = s.indexOf('*',x);
}
sb.append(s.substring(x));
return sb.toString();
}
/**
* Checks to see if a provided file name matches the provided DOS pattern.
*/