想要用Java做一个文件搜索的功能,该如何实现???我没有思路啊!如何遍历文件和目录呢????

解决方案 »

  1.   

    public static void pathDFS(File file) {
    if (file.isDirectory()) {
    File[] fileList = file.listFiles();
    for (int i = 0; i < fileList.length; i++) {
    pathDFS(fileList[i]);
    }
    } else if (file.isFile()) {
    dosomething(file);

    }
      

  2.   

    f为初始路径
             public void searchFile(File f)
    {
    if(f.isDirectory()){
    File[] files=f.listFiles();
    int len=files.length;
    for(int i=0;i<len;i++)
    searchFile(files[i]);
    }
    else{
    String name=f.getName();
    System.out.println(f.toString());
    if(f.toString().endsWith(".dsp")){//这里我是找dsp文件
    name=name.substring(0,name.length()-4);
    System.out.println(name);
    }
    }
    }
      

  3.   


    import java.util.ArrayList;
    import java.io.File;public class FindFile {  static ArrayList findFilePath = new ArrayList();  private static void findFile(String path, String findFileName) {
        File file = new File(path);
        if (file.isDirectory()) {
          File[] files = file.listFiles();
          for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
              if (files[i].getName().equals(findFileName)) {
                findFilePath.add(files[i].getPath());
              }
            }
            else {
              findFile(files[i].getPath(), findFileName);
            }
          }
        }
      }  public static void main(String[] as) {
        findFile("D:\\aaa", "aaa.tex");
        for (int i = 0; i < findFilePath.size(); i++) {
          System.err.println("" + findFilePath.get(i).toString());
        }  }
    }
      

  4.   

    到google搜索lucene 然后就知道了
      

  5.   

    JAVA实现搜索,对平台有什么要求吗?
      

  6.   

    lucene 是一个搜索引擎的开放源码,可以作为一个开发包来使用