如何从文件列表中搜索和计算模式?

时间:2018-03-26 03:06:30

标签: java file search matcher

我有一个文件夹中的文件列表,需要搜索3个模式/单词并且计数为no。特定类型的出现。例如,在这3种模式中,我将使用颜色(红色,蓝色,绿色等),并且每种颜色都有START(第一个索引)和END(最后一个索引)。我需要为每种颜色获取每个索引的计数。

希望我的问题很明确,请让我知道如何实现它。

2 个答案:

答案 0 :(得分:0)

使用以下源代码获取目录中的所有文件,并获取文件名为

的文件

代码 -

package han.code.development;

import java.io.File;
import java.io.FileFilter;

public class SearchAndCount 
{
    public String getAllFiles()
    {
        File dir=new File("D:\\Haneef\\exe java");
        String fileName = null;
        StringBuilder sb=new StringBuilder();
        System.out.println("Get all files from directory - "+dir);
        if(dir.isDirectory()==true)
        {
             File[] files=dir.listFiles();
             for(File getfile:files)
             {
                  fileName=getfile.getName();
                  sb.append(fileName);
                  sb.append("\n");
             }
             return sb.toString();
        }
        return null;
   }
   public String getFile(String str)
   {
       File dir = new File("D:\\Haneef\\exe java");;
       File[] files;
       String retstr;  
       StringBuilder sb=new StringBuilder();
       System.out.println("Get all files from directory - "+dir+" in the name of "+str);
       try 
       { 
           FileFilter filter = new FileFilter()
           {
               @Override
               public boolean accept(File pathname)
               {
                   if (pathname.getName().startsWith(str))
                   {
                       return true;
                   }
                   return false;
               }
           };
           files = dir.listFiles(filter);
           for(File file:files) 
           {
               retstr=file.getName();
               sb.append(retstr);
               sb.append("\n");
           }
           return sb.toString();
      } 
      catch(Exception e)
      {
          e.printStackTrace();
      }
      return null;
   }
   public static void main(String[] args)
   {
      SearchAndCount searchfile=new SearchAndCount();
      System.out.println(searchfile.getAllFiles());
      System.out.println(searchfile.getFile("Fil"));
   }
}

输出 -

 Get all files from directory - D:\Haneef\exe java
 File1.txt
 File2.txt
 File3.txt
 TextFile.txt

 Get all files from directory - D:\Haneef\exe java in the name of Fil
 File1.txt
 File2.txt
 File3.txt

希望这会有所帮助!!

答案 1 :(得分:0)

我希望以下代码能满足您的要求

package han.code.development;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatterSearch 
{
    public String getAllFiles()
    {
        File dir=new File("D:\\Haneef\\exe java");

        String fileName = null;
        StringBuilder sb=new StringBuilder();

        if(dir.isDirectory()==true)
        {
            File[] files=dir.listFiles();
            for(File getfile:files)
            {
                 fileName=getfile.getPath();
                 sb.append(fileName);
                 sb.append(";");
            }
            return sb.toString();
        }
        return null;
   }
   public List patternSearch(String fileName,String txt) throws FileNotFoundException
   {
       Pattern pattern=null;
       int linenum=0;
       Scanner fileScanner=new Scanner(new File(fileName));
       String fileTxt=fileScanner.nextLine();
       pattern=pattern.compile(txt);
       Matcher matcher=pattern.matcher(fileTxt);
       String fileNames=getAllFiles().toString();
       List lst=new ArrayList();
       StringBuffer sb=new StringBuffer();
       if(matcher.find())
       {
            sb.append("FileName : "+fileName+" ; ");
            sb.append("find : "+pattern+" ; ");
            sb.append("Start : "+matcher.start()+" ; ");
            sb.append("End : "+matcher.end()+" ; ");
            lst.add(sb.toString());
       }
       return lst;
  }

  public List patternSearchClient() throws FileNotFoundException
  {
      String fileNames=getAllFiles();
      String[] fileName=fileNames.split(";");
      List lst=new ArrayList();
      for(int i=0;i<fileName.length;i++)
      {
           lst.add(patternSearch(fileName[i], "haneef"));
      }
      return lst;
  }

  public static void main(String[] args) throws FileNotFoundException
  {
      PatterSearch searchfile=new PatterSearch();
      System.out.println("Get all files from directory \n ");
      System.out.println(searchfile.getAllFiles()+" \n ");
      System.out.println(searchfile.patternSearchClient());
  }
}


 Output - 

 Get all files from directory 

 D:\Haneef\exe java\File1.txt;D:\Haneef\exe java\File2.txt;D:\Haneef\exe java\File3.txt;D:\Haneef\exe java\TextFile.txt; 

 [[FileName : D:\Haneef\exe java\File1.txt ; find : haneef ; Start : 6 ; End : 12 ; ], [], [], [FileName : D:\Haneef\exe java\TextFile.txt ; find : haneef ; Start : 0 ; End : 6 ; ]]