Results 1 to 4 of 4
  1. #1
    superdry is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Post traverse directory search string in files help please

    Hi folks,

    Im trying to write a program that accepts two input parameters:

    1. The string to search for
    2. The directory to search in

    For each file in the specified directory the program counts the number of times the specified string occurs in the file.
    I'm assuming that all the files in the directpry are .text.

    Java Code:
     
     //this program searches  a file and returns the number of lines in the file and how many times the searched word is found.
      
      import java.io.*;  
     import java.util.regex.*;  
     public class words 
    {  
         	private static void traverse(String directory) 
    	 {
    		File dir = new File(directory);
                    if (dir.isDirectory())
    		{
                           String[] children = dir.list();
                           for (int i=0; i<children.length; i++) 
    		       {
    			     searchPattern(children[i]);
    		        }
                     }
    
             }
     
         public static void searchPattern(String children){ 
            try{  
    		File fw = new File(children); 
    		System.out.println(fw.exists());  
    			
    		BufferedReader pw = new BufferedReader(new FileReader(fw));  
    		int count = 0;  
    		String s = null;  
    		System.out.println("Please enter a string to search");
    		String sear = Console.readString();
    		Pattern p =  Pattern.compile(sear);  
    			while((s = pw.readLine()) != null)
    			{    
    								  
    			     Matcher m = p.matcher(s);  
    			     if(m.find())
    			     {
    					count ++;
    			      };
    								 					   
    			}
    				System.out.println(count);
    						
    			}
    				
    		 catch(IOException e)
    			 {  
    				 e.printStackTrace();                
    			 }  
         
    	 }
    	 	 
         public static void main(String... args){ 
    			System.out.println("Please enter a directory to search");
    			String directory = Console.readString();
    			
    			traverse(directory);
    			words f = new words();  
                            f.searchPattern(directory); 
    		  }  
     }

    I seem to have hit a brick wall, maybe I've been looking at it too long but my java is not that strong anyway. The traverse method works on its own, the search method works on its own but I cant get them to work together or to give me the number of times the string occurs in each file.

    Any help would be greatly appreciated.
    Cheers

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    What you mean by work them on it's own way?

    Simply the logic should be once you find the directory path you've to hang on it until there are no more text files. Once all the file searching is completed, move to the next directory and so on... So where you stuck with? Can you explain a bit more...

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    So make your searchPattern method return the number of hits and then you can count the total of hits in all the files in your traverse method.
    Better would be to create a SearchResult class that contains the number of hits, the file being searched and any other bits you think necessary. You then build a List of them as you search each in your traverse method which would then a List<SearchResult>.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. (Recursively?) Getting Files in a Directory
    By Singing Boyo in forum New To Java
    Replies: 2
    Last Post: 05-21-2009, 08:37 AM
  2. load all files in a directory
    By moomoo in forum New To Java
    Replies: 1
    Last Post: 04-21-2008, 10:18 AM
  3. How can I get list of files in a directory
    By karma in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 11:20 PM
  4. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  5. how to find files in given directory
    By cecily in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 04:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •