当前位置: 技术问答>java相关
怎样取得某目录下的文件名,支持通配符(?,*)
来源: 互联网 发布时间:2015-01-06
本文导语: 怎么取得某目录下的文件名(不包括子目录),支持通配符(?,*),就像windows的search功能一样;而且取到文件名后怎么按文件名排序,怎么按最后修改时间排序? 请哪位贴段代码上来? (Java的File类不太好用,我...
怎么取得某目录下的文件名(不包括子目录),支持通配符(?,*),就像windows的search功能一样;而且取到文件名后怎么按文件名排序,怎么按最后修改时间排序?
请哪位贴段代码上来? (Java的File类不太好用,我试了试,没能实现上述功能)
请哪位贴段代码上来? (Java的File类不太好用,我试了试,没能实现上述功能)
|
//看看这段程序。
//好像现在还没有现成的可以用。
import java.io.*;
/**
* 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
//好像现在还没有现成的可以用。
import java.io.*;
/**
* 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