aspose file tools*
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
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: 41900
    
  63
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: 41900
    
  63
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: 19697
    
  20

http://www.google.com


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: searching for a content in a file