You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Saranya Balasubramanian <Sa...@infosys.com> on 2007/03/30 11:07:13 UTC

Search on file contents using struts

Hi all,

 

I want to implement search on file contents for keywords using struts.
Presently, I am reading the file contents line by line and checking for
the occurrence of the keywords. Below is the code I use for searching a
file.

br = new BufferedReader(new FileReader(file));     

                                    while ((line = br.readLine()
)!=null){

                                                line =
line.toLowerCase();

                                                for (int i=0;
i<keyword.length; i++){

                                                            if
(line.indexOf(keyword[i]) != -1 ){

 
containsKeyword = true;

 
System.out.println("\t"+file.getName() + " - selected." );

 
System.out.println("\t\t"+line );

 
return containsKeyword;

                                                            }

                                                }           

}

This is the easiest idea that stuck me first but I'm worried about its
efficiency as this function will be called once for every file and there
are many files in the search location.

 

And to locate the files I traverse the directories recursively till I
find all the files. Even this works pretty slowly for huge directory
structures. I'm pasting the code below:

public ArrayList getFileList(String dir, String keywords){

                        System.out.println(dir);

                        try {

                                    File file = null;

                                    if(dir !=null){ 

                                                file= new File(dir);

                                    }

                                    FilenameFilter filter = new
FileFilter();

 

                                    File[] list =
file.listFiles(filter);

                                    if(list != null) {

                                                for(int loopCount =
0;loopCount < list.length;loopCount++) {

                                                            String
fileName = list[loopCount].getName();

 
if(list[loopCount].isFile()) {

 
/*Search*/

 
if (isPresent(keywords, list[loopCount])){

 
LabelValueBean bean = new
LabelValueBean(fileName/*.substring(0,fileName.indexOf("."))*/,list[loop
Count].getAbsolutePath());

 
fileList.add(bean);

 
}

                                                            } else {

 
this.getFileList(list[loopCount].getAbsolutePath(), keywords);

                                                            }

                                                }

                                    }

                        }

                        catch(Exception ioe) {

                                    d_log.error("SearchImpl.java : IO
Exception while getting list of directories or files." , ioe);

                        }

                        

                        return fileList;                

            }

 

Please let me know if there's a better way to implement this function.

 

Thanks,

Saranya



**************** CAUTION - Disclaimer *****************
This e-mail contains PRIVILEGED AND CONFIDENTIAL INFORMATION intended solely for the use of the addressee(s). If you are not the intended recipient, please notify the sender by e-mail and delete the original message. Further, you are not to copy, disclose, or distribute this e-mail or its contents to any other person and any such actions are unlawful. This e-mail may contain viruses. Infosys has taken every reasonable precaution to minimize this risk, but is not liable for any damage you may sustain as a result of any virus in this e-mail. You should carry out your own virus checks before opening the e-mail or attachment. Infosys reserves the right to monitor and review the content of all messages sent to or from this e-mail address. Messages sent to or from this e-mail address may be stored on the Infosys e-mail system.
***INFOSYS******** End of Disclaimer ********INFOSYS***

[OT] Re: Search on file contents using struts

Posted by Dave Newton <ne...@yahoo.com>.
--- Saranya Balasubramanian wrote:
> Please let me know if there's a better way to
> implement this function.

http://lucene.apache.org/java/docs/ would be one
solution.

d.



 
____________________________________________________________________________________
Never miss an email again!
Yahoo! Toolbar alerts you the instant new Mail arrives.
http://tools.search.yahoo.com/toolbar/features/mail/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org