上  http://forum.java.sun.com 上看看,有范例

解决方案 »

  1.   

    to binriyue:
        不是,我是想在无界面的程序中使用上述功能,不过用java的File类来实现很麻烦(比如windows下的文件名是不区分大小写的,而java的排序功能是分大小写的;按最后修改时间排序要取每个文件的最后修改时间,然后再排序;支持通配符要自己实现FilenameFilter接口),我想知道java中有没有现成的类,有没有简单点的方法。
      

  2.   

    //看看这段程序。
    //好像现在还没有现成的可以用。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 <= 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.
    */public static boolean match(String pattern,String fileName){
    pattern = pattern.trim();
    pattern = toMinimalPattern(pattern);
    pattern = pattern.toUpperCase();
    char[] ca1 = pattern.toCharArray();
    fileName = fileName.trim();
    fileName = fileName.toUpperCase();
    char[] ca2 = fileName.toCharArray();
    return match(ca1,0,ca2,0);
    }private static boolean match(char[] pattern,int px, char[] name, int nx){
    while (px < pattern.length){
     char pc = pattern[px];
     if (pc == '*'){
     if (px >= (pattern.length - 1))
     return true;
     int x = nx;
     while (x < name.length){
     if (match(pattern,px+1,name,x))
      return true;
     x++;
    }
     px++;
     continue;
    }
    if (nx >= name.length)
     return false;
    if (pc != '?'){
    if (pc != name[nx])
     return false;
    }
     px++;
     nx++;
    }
    return (nx >= name.length);
    }public static boolean isMatch(String mask, String nick) {
    mask = mask.trim();
    if (mask.equalsIgnoreCase("*")) {
    return true;
    }
    if (mask.startsWith("*")) {
    int end = -1;
    if (mask.indexOf("*",mask.indexOf("*") + 1) == -1) {
    end = mask.length();
    } else {
    end = mask.indexOf("*",mask.indexOf("*") + 1);
    }
    if (end == -1) {
    return false;
    }
    String tmask = mask.substring(1, end).trim();
    if (nick.indexOf(tmask) == -1) {
    return false;
    } else {
    mask = mask.substring(end).trim();
    nick = nick.substring(nick.indexOf(tmask)).trim();
    return isMatch(mask, nick);
    }
    }
    int end = -1;
    if (mask.indexOf("*") == -1) {
    end = mask.length();
    } else {
    end = mask.indexOf("*");
    }
    if (end == -1) {
    return false;
    }
    String tmask = mask.substring(0, end).trim();
    if (nick.startsWith(tmask)) {
    if (end == mask.length()) {
    return true;
    }
    mask = mask.substring(end).trim();
    nick = nick.substring(tmask.length()).trim();
    return isMatch(mask, nick);
    } else {
    return false;
    }
    }
    }
      

  3.   

    to bootcool:
        你的MYFileFilter类不好用,我试了试,连"c:\\"这么简单的目录名下的文件名都读不出来。
      

  4.   

    //这是找文件的。
    //不过是找后缀名。
    //我也在想如何加入通配符查找
    //不过我想MYFileFilter上面的方法还是有用的。import java.io.*; class SearchFile{ public static void main(String args[]){ 
      File aDirectory = new File("C:/My Documents/My Pictures");
      String extension =".jpg"; 
      find(aDirectory,extension,true); 
    } static void find(File currentDirectory,String extension,boolean 
     includeSubdirectories){  if(!currentDirectory.isDirectory()){ 
      System.out.println (currentDirectory.toString()+ " doesnot exists " ); 
      return; 
     } File[] currentDirectoryFileNames = currentDirectory.listFiles(); 
    for(int i=0;i<currentDirectoryFileNames.length;i++){ 
     if(currentDirectoryFileNames[i].isFile()){ 
      String s= currentDirectoryFileNames[i].getName(); 
      int index = s.indexOf('.'); 
      if(!(index <0)){ 
        String extensionofThisFile = s.substring(index); 
         if(extensionofThisFile.equals(extension)){ 
          System.out.println(currentDirectoryFileNames[i]); 
       } 
     } 
     } 
    }   if(includeSubdirectories){ 
       for(int i=0;i<currentDirectoryFileNames.length;i++){ 
        if(currentDirectoryFileNames[i].isDirectory()){ 
         find (currentDirectoryFileNames[i],extension,includeSubdirectories); 
        } 
       } 
      } 
     } 

      

  5.   

    //ghw老兄,用这个试试。
    //但是还是有一些形式的文件名搜不出。
    //文件名是这样的:
    //gigi1.jpg,gigi2.jpg,gigi3.jpg,bgigi.jpg,tgirl.jpg....
    //import java.io.*;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();
    }public static boolean match(String pattern,String fileName){
    pattern = pattern.trim();
    pattern = toMinimalPattern(pattern);
    pattern = pattern.toUpperCase();
    char[] ca1 = pattern.toCharArray();
    fileName = fileName.trim();
    fileName = fileName.toUpperCase();
    char[] ca2 = fileName.toCharArray();
    return match(ca1,0,ca2,0);
    }private static boolean match(char[] pattern,int px, char[] name, int nx){
    while (px < pattern.length){
     char pc = pattern[px];
     if (pc == '*'){
     if (px >= (pattern.length - 1))
     return true;
     int x = nx;
     while (x < name.length){
     if (match(pattern,px+1,name,x))
      return true;
     x++;
    }
     px++;
     continue;
    }
    if (nx >= name.length)
     return false;
    if (pc != '?'){
    if (pc != name[nx])
     return false;
    }
     px++;
     nx++;
    }
    return (nx >= name.length);
    }public static boolean isMatch(String mask, String nick) {
    mask = mask.trim();
    if (mask.equalsIgnoreCase("*")) {
    return true;
    }
    if (mask.startsWith("*")) {
    int end = -1;
    if (mask.indexOf("*",mask.indexOf("*") + 1) == -1) {
    end = mask.length();
    } else {
    end = mask.indexOf("*",mask.indexOf("*") + 1);
    }
    if (end == -1) {
    return false;
    }
    String tmask = mask.substring(1, end).trim();
    if (nick.indexOf(tmask) == -1) {
    return false;
    } else {
    mask = mask.substring(end).trim();
    nick = nick.substring(nick.indexOf(tmask)).trim();
    return isMatch(mask, nick);
    }
    }
    int end = -1;
    if (mask.indexOf("*") == -1) {
    end = mask.length();
    } else {
    end = mask.indexOf("*");
    }
    if (end == -1) {
    return false;
    }
    String tmask = mask.substring(0, end).trim();
    if (nick.startsWith(tmask)) {
    if (end == mask.length()) {
    return true;
    }
    mask = mask.substring(end).trim();
    nick = nick.substring(tmask.length()).trim();
    return isMatch(mask, nick);
    } else {
    return false;
    }
    }
    }
      

  6.   

    //如果你用的是jdk1.4,可以这样做。import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileFilter; //not java.io.FileFilter
    import java.io.File;
    import java.util.*;
    import java.util.regex.*;//case insensitivepublic 
    class RegexFilter extends FileFilter{
    public RegexFilter(String description, String regex) {
          m_description = description;
    m_pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    }
    public boolean accept(File f) {if (f.isDirectory()) return true;
    String name = f.getName();
    System.out.println("comparing " + name);
    Matcher matcher = m_pattern.matcher(name);
    return matcher.matches();
    }
    public String getDescription(){return m_description;}
    private Pattern m_pattern;
    private String m_description;
    public static class Test{
    public static void main(String[] argv){
    JFileChooser chooser = new JFileChooser(new File("."));
    chooser.setDialogTitle("Choose an image file");
    chooser.setMultiSelectionEnabled(false);
    RegexFilter filter = new RegexFilter("*java", ".*\\.java");
    chooser.addChoosableFileFilter(filter);
    chooser.showOpenDialog(null);
    System.out.println(chooser.getSelectedFile());
    }
    }
    }
      

  7.   

    bootcool(bootcool) :
       好热情噢!
      

  8.   

    非常感谢bootcool,明天到公司试试你的方法。
      

  9.   

    to bootcool:
        你的方法我昨天试了试,确实有一些文件查不出;jdk1.4还是beta版呢,还是不用了。
        非常感谢你,贴子再留一天,我明天就给分。    我是初学java(一个多月),以后还请你多多指教。