This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes searching for 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "searching for a content in a file" Watch "searching for a content in a file" New topic
Author

searching for a content in a file

N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
Hi i want to search for a content in different types of files like msword,text,excel,pdf,ppt etc...

Could anybody tell me what is the optimum code to search for a content in any type of file using BufferReader

Right now i am using following code where "text" is the content to be searched but the problem here is i am getting the filename even though if i give for wrong content.

BufferedReader in = null;
try
{
in = new BufferedReader( new FileReader( f ) );
String line;

while ( ( line = in.readLine() ) != null )
{
if ( line.toLowerCase().indexOf( text ) != -1 )
{
return true;
}
}

}
catch ( IOException e )
{
cLog.error( cConfiguration.getFormattedString("search.error",new Object[]{f}), e );
return false;
}
finally
{
if ( in != null )
{
try { in.close(); } catch ( IOException e ) {}
finally {}
}
}
return false;
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
Text files are the only ones you'll be able to search reliably using this approach. The other formats are binary, structured files that are not amenable to this kind of simplistic approach.

I'd look into using a search library like Lucene for this, but you'll still need to use particular libraries to get at the contents of those formats, like Apache POI for DOC, XLS and PPT, and PDFBox for PDFs.


Ping & DNS - my free Android networking tools app
N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
Thanks for your reply could you please give me small example how we can search binary files.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
Apache POI has extensive documentation and examples online. For PDFBox, look for "text extraction" on its web site.
N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
Thank you very much could you please give me the website URL.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

http://www.google.com


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: searching for a content in a file
 
Similar Threads
File IO not returning certain characters.
check values in a csv file in all files in a directory
Help with LineNumber Reader class
file search using scanner
read content of text file which is in jar