Results 1 to 4 of 4
- 08-24-2009, 12:50 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
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
- 08-27-2009, 06:12 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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...
- 08-27-2009, 08:55 AM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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>.
- 08-28-2009, 03:51 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Using the File class you can do this easily.
Similar Threads
-
(Recursively?) Getting Files in a Directory
By Singing Boyo in forum New To JavaReplies: 2Last Post: 05-21-2009, 08:37 AM -
load all files in a directory
By moomoo in forum New To JavaReplies: 1Last Post: 04-21-2008, 10:18 AM -
How can I get list of files in a directory
By karma in forum New To JavaReplies: 2Last Post: 12-14-2007, 11:20 PM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
how to find files in given directory
By cecily in forum New To JavaReplies: 1Last Post: 08-05-2007, 04:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks