It's not a secret anymore!*
The moose likes Beginning Java and the fly likes directory searching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "directory searching" Watch "directory searching" New topic

directory searching

kajaja noto

Joined: Mar 03, 2005
Posts: 8
i have a program that searches a document for a string, however i am having trouble trying to make the program able to search a folder of documents with sub-directories. i am using a fileDialog to pass the path of the document to the program, i want to be able to pass the a directoy path instead of a file path.

here is my code for passing the path to the program as a string:

my code for searching a file:

any ideas will be much appreciated.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Where exactly are you encountering problems? Does the code compile? When you run it what happens? How is the behavior different from what you expect?

Posting some code helps in explaining what you are trying to do, but you also need to answer the above questions to explain what actually happens. This will make it easier for us to answer your question.

Keep Coding!


Java API Documentation
The Java Tutorial
kajaja noto

Joined: Mar 03, 2005
Posts: 8
my code compiles and it works how i expect it to work. my aim is to make a search engine program, at the moment my program searches a text file for a string and it will print out the line of occurrence. my next step is to allow my program to be able to search a folder of files, instead of just one file at a time. in the end i want to be able to rank each file by no. of string occurrences. but for now i am having trouble trying to implement the directory searching, i want it to be able to read a folder for it's contents, and then i can recursively call my searchfile method to search the document if the file at that instance is a document or if it is folder then to step into the sub-directory.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Check out This link that describes how to do this.

Essentially, you use the isDirectory() method on the File object to determine whether it's a directory or not. If it is, you can list it's contents using the list() method.


Consultant, Sima Solutions
kajaja noto

Joined: Mar 03, 2005
Posts: 8
thankyou very much for your advice, after i have used the list method, how will access everything in the list, i.e. access the files/folders in the folder, i am sorry i am asking silly questions it's just that i am new to java and unfortunatrly struggling with it. if anyone can post an example of how i can do this, i would be very grateful.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9044
Kajaja, What do you get back when you use the list method of the File class? How would you use it to access everything in the list?

Also, welcome to JavaRanch. We need to use two names separated by a space. You can change your display name using this link. Thanks.
[ March 05, 2005: Message edited by: Marilyn de Queiroz ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
kajaja noto

Joined: Mar 03, 2005
Posts: 8
i am sorry about my name, i was not aware of this rule.... i have been trying to use the list method but i have been unsuccessful, i am not a java programmer and i am really struggling. if you could post an example of how i could do this, i will be very grateful.
It is sorta covered in the JavaRanch Style Guide.
subject: directory searching
Similar Threads
FileDialog with Filter!!!
count how many tokens are in the first line
doubt about string replacement
setting up the size of a JTable to the size of a vector
How can I find out how many lines there are?