File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes searching a content in a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "searching a content in a file" Watch "searching a content in a file" New topic

searching a content in a file

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi All,

suppose,there are(example D drive) some files which are having paragraph ...

if i give some word in search button..then i i want the file/files which is having the particular word...

how to do it?

please anyone can explain me

thanks & regards,
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

This is an example of a recursive method - if the File is a directory, the same method is called with the new File. This will end if either a File is a file or a directory is empty; then the next File will be searched.

That leaves the implementation of the contains method. You'll have to read in each line and check for presence until the word is found. Look for BufferedReader and its readLine method for reading the File. You can use String's indexOf method to check if the word is present.

How To Ask Questions How To Answer Questions
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

Apache lucene is a good text search engine if you need a thrid party library to do the search for you.

apigee, a better way to API!
I agree. Here's the link:
subject: searching a content in a file
jQuery in Action, 3rd edition