文件目录使用名称,分机,内容,修改日期等进行搜索

时间:2014-05-21 22:49:13

标签: java file file-io directory

我试图建立一个搜索引擎来搜索我的计算机给定一条路径,我决定使用任何选项。所以,如果我决定寻找文件名" resume"使用扩展名" pdf",它将为我提供目录中的文件,包括子文件。我无法弄清楚我应该怎么做。这就是我所拥有的,但现在我就是这样。

import java.io.*;
import java.util.*;
import java.text.*;

public class mySearch
{
   private final static int path = 0;
   private final static int name = 1;
   private final static int ext = 2;
   private final static int content =3;
   private final static int date = 4;
   private static Scanner kb;

   public static void main(String [] args)
   {
      ArrayList<String> result = new ArrayList<String>();

      Scanner kb = new Scanner(System.in);
      System.out.println("Search by path, name, extension, content and date.\n\n");

      System.out.print("Enter starting directory for the search (like c:" + "\\" + "temp): ");
      String direct = kb.nextLine();
      File item = new File(direct);

      System.out.print("Enter the file name (like myFile or enter for all): ");
      String fileN = kb.nextLine();

      System.out.print("Enter the file extension (like txt or enter for all): ");
      String ext = kb.nextLine();

      System.out.print("Enter content to search for (like cscd211 or enter for any): ");
      String content = kb.nextLine();

      System.out.print("Enter last modified date (like 11/21/2013 or enter for any): ");
      String dat = kb.nextLine();

      if (!(dat.equals("")))
      {
         long d = Long.parseLong(dat);
         search(item, fileN, ext, content, d, result);
      }
      else
      {
         long k = 0;
         search(item, fileN, ext, content, k, result);
      }

      int count = result.size();
      if(count == 0)
      {
         System.out.println("\n No Result Found!");

      }
      else
      {
         System.out.println("\n Found " + count + " result!\n");
         for(String matched : result)
         {
            System.out.println("Found: " + matched);
         }
      }

   }

   private static void search(File item, String fileName, String extension, String content, long modDate, ArrayList<String> results)
   {
      String sWork = null;
      String base = "";
      String ext = "";

      if (item.isDirectory())
      {
         File[] name = item.listFiles();

         if (name != null)
         {
            for (File names : name)
            {
               search(names, fileName, extension, content, modDate, results);
            }
         }

      }

      else
      {
         String fNameFull = item.toString();

         String fNameShort = item.getName();

         int loc = fNameShort.lastIndexOf('.');

         if (loc > 0)
            ext = fNameShort.substring(loc + 1);

         if (loc > 0)
            base = fNameShort.substring(0, loc);

         else
            base = fNameShort;

         if (extension.equals("") || ext.equalsIgnoreCase(extension))
         {
            if (modDate == 0 || item.lastModified() >= modDate)
            {
               if (fileName.equals("") || fileName.equalsIgnoreCase(base))
               {
                  if (content.equals(""))
                  {
                     results.add(fNameFull);
                  }

                  else
                  {
                     try
                     {
                        Scanner fin = new Scanner(item);
                        sWork = fin.findWithinHorizon(content, 0);
                        if (sWork != null)
                           results.add(fNameFull);
                     }
                     catch (IOException e)
                     {
                        System.out.println("Error on " + fNameFull);
                     }
                  }
               }
               else 
               {
                  try
                  {
                     if (content.equals(""))
                     {
                        Scanner fin = new Scanner(item);
                        sWork = fin.findWithinHorizon(fileName);
                        if (sWork != null)
                           results.add(fNameFull);
                     }
                     else
                     {
                        Scanner fin = new Scanner(item);
                     }

                  }
                  catch (IOException e)
                  {
                     System.out.println("Error on " + fNameFull);
                  }
               }
            }
         }
      }
   }
}

1 个答案:

答案 0 :(得分:0)

apache.commons.io.FileUtils.iterateFiles()。

它会走树,你可以指定过滤器。