Java Program to Count Number of Files

50,000 HTML files are in a folder on a windows machine. Among them, content of some files
(not the file names) contain the sentence "SocioMaC-Jus' Follow!". Write a Java program to count the number of such files.

Questions by anjankumar_m

Showing Answers 1 - 9 of 9 Answers

Sarje

  • Aug 20th, 2009
 

You can count the number of .html .htm files having any pattern suppose "Ram" using following code.


import java.io.*;
import java.util.Scanner;
public class HtmlFileFilter implements FileFilter
{
       public boolean accept(File pathname)
       {
              if (pathname.getName().endsWith(".html")) return true;
              if (pathname.getName().endsWith(".htm")) return true;
              return false;
        }
        public static void main(String args[])throws IOException
        {
            int counter = 0;
            FileFilter ob = new HtmlFileFilter();
            File dir = new File("E:/Sarje");
            File[] files = dir.listFiles(ob);
            for(File f : files)
            {
                InputStream fis = new FileInputStream(f);
                Scanner scan = new Scanner(fis);
                while(scan.hasNext())
                {
                    if(scan.next().equals("Ram"))
                    counter++;
                }
            }
            System.out.println("Number of files:"+counter);
        }
}

kneebrains

  • Oct 30th, 2009
 

If you need to _search_ random strings - use lucene. Otherwise i think sarje has the answer.
Can you not write the text parsing in the file filter ?

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions