import java.io.File;
import java.io.FileFilter;public class Mtest { public static void main(String[] args) {
File bb = new File("D:\\DELL");
File[] files = bb.listFiles(new FileFilter(){ public boolean accept(File pathname) {
                int index = pathname.getName().lastIndexOf(".");
                if ("java".equals(pathname.getName().substring(index + 1))) {
                    return true;
                } 
                return false;
}});
}}中间的accept函数是过滤的条件,自己随便改.

解决方案 »

  1.   

    File file = new File(path); 
    String[] files = file.list();
    if (files != null) {
    int order = 0;
    int length = files.length; 
    String jarName = null; 
    while(order < length) { 
    jarName = files[order++];
    if (jarName.endsWith(".jar")) {
      

  2.   

    参考下面的例子
    JavaFilter.javaimport java.io.*;// This class implements a filename filter that only allows
    // files that end with .javapublic class JavaFilter extends Object implements FilenameFilter
    {
         public JavaFilter()
         {
         }     public boolean accept(File dir, String name)
         {// Only return true for accept if the file ends with .java
              return name.endsWith(".java");     }
    }ListJava.javaimport java.io.*;public class ListJava extends Object
    {
         public static void main(String[] args)
         {// Create a File instance for the current directory
              File currDir = new File(".");// Get a filtered list of the .java files in the current directory
              String[] javaFiles = currDir.list(new JavaFilter());// Print out the contents of the javaFiles array
              for (int i=0; i < javaFiles.length; i++) {
                   System.out.println(javaFiles[i]);
              }
         }
    }
      

  3.   

    回: whyxx(越来越觉得自己什么都不会) 
    你的代码很好~谢谢~顺便问一下~假设d:盘有不同的文件夹~该如何遍历每个文件价也就是每条路径然后过滤出想要的文件呢?重点是自动遍历每一条存在的路径?给个思路或代码~解决了再送50~谢谢!
    honkyjiang(老蒋) 谢谢!
    sunjy() ( )  File currDir = new File("."); 这行改一下结果也一样!谢谢!
      

  4.   

    当然是用递归来遍历了
    import java.io.*;
    class A
    {
    public static void main(String[] args)
    {
    File file=new File("e:\\1000");
             A a=new A();
    a.read(file); }
    public void read(File f)
    {
    File[] files=f.listFiles();
    for(int i=0;i<files.length;i++)
    {
    File tmpFile=files[i]; if(tmpFile.isDirectory())
    this.read(tmpFile);
    else
    System.out.println("file:"+tmpFile);
    }
    }}
    这个小程序只是遍类所有子目录的文件名的
    自己加上楼上的new FileFilter()过滤条件就行了