This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

directory searching

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

Layne
 
kajaja noto
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Merrill
 
kajaja noto
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
kajaja noto
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic