GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 923 of 928    Print  
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.



  
Total Answers and Comments: 3 Last Update: November 01, 2009     Asked by: anjankumar_m 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: bh2008
 
Implement java.io.FileFilter interface and accept method.
Use java.io.File class listFiles(FileFilter) method to filter.

Above answer was rated as good by the following members:
ugesh.gali
October 08, 2008 01:23:24   #1  
bh2008 Member Since: June 2008   Contribution: 3    

RE: Java Program to Count Number of Files
Implement java.io.FileFilter interface and accept method.
Use java.io.File class listFiles(FileFilter) method to filter.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 20, 2009 07:15:10   #2  
Sarje Member Since: August 2009   Contribution: 62    

RE: Java Program to Count Number of Files
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);
}
}

 
Is this answer useful? Yes | No
October 30, 2009 12:16:50   #3  
kneebrains Member Since: October 2009   Contribution: 2    

RE: Java Program to Count Number of Files
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 ?

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape