The Artima Developer Community
Sponsored Link

Java Answers Forum
search techniques in java

3 replies on 1 page. Most recent reply: Feb 17, 2004 8:17 PM by Mike Sandman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
swapna

Posts: 1
Nickname: so
Registered: Feb, 2004

search techniques in java Posted: Feb 4, 2004 12:49 AM
Reply to this message Reply
Advertisement
How to write the code for searching the text in html pages in jsp or java. Actually i had lots of html links and i have to enter the keyword(string) in the search box and that keyword should check all of my html pages and that that links should be displayed on my browser. I'm unable to write this code in jsp, if u had the solution please help me to solve this problem.
Thanks and Regards,
swapna.


Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: search techniques in java Posted: Feb 16, 2004 8:20 AM
Reply to this message Reply
Hi Here is the sample code for the same....



import java.io.*;
import java.util.StringTokenizer;


public class SearchText
{
public static void main(String[] arguments)
{
int exitCode = 0;
try
{
//String rootURL = "C:\\Viswa-GB\\MyJava\\";
String rootURL ="C:\\TODO\\";

File dir = new File(rootURL);
String[] children = dir.list();
if (children == null)
{
// Either dir does not exist or is not a directory
}
else
{
for (int i=0; i<children.length; i++)
{
// Get filename of file or directory
String filename = children;
String line = null;
//System.out.println(rootURL+filename + "-------------------------------") ;
BufferedReader file = new BufferedReader(new FileReader(rootURL+filename));
while ((line = file.readLine()) != null)
{
System.out.println(line);
boolean found = line.contentEquals(new StringBuffer("contains"));
if(found)
{
System.out.println("String FOUND at" + rootURL + filename);
//System.exit(exitCode);
}
else
{
System.out.println("String NOT FOUND" + i);
}
}
file.close();
}
}

}
catch (Exception e)
{
System.out.println("Error -- " + e.toString());
// exitCode = -1;
}
//System.exit(exitCode);

}
}

Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: search techniques in java Posted: Feb 17, 2004 12:05 AM
Reply to this message Reply
Hi
I have made some corrections please use this code to serch a text.


import java.io.*;
import java.util.StringTokenizer;


public class SearchText
{
public static void main(String[] arguments)
{
int exitCode = 0;
try
{
String rootURL = "C:\\1Viswa-GB1\\Test\\";
String searchText = "Web";
File dir = new File(rootURL);
String[] children = dir.list();
if (children == null)
{
// Either dir does not exist or is not a directory
}
else
{
for (int i=0; i<children.length; i++)
{
// Get filename of file or directory
String filename = children;
String line = null;
BufferedReader file = new BufferedReader(new FileReader(rootURL+filename));
while ((line = file.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens())
{
boolean found = searchText.equalsIgnoreCase(st.nextToken());
if(found)
{
System.out.println("String Found @ " + rootURL + filename);
}
}
}

file.close();
}
}

}
catch (Exception e)
{
System.out.println("Error -- " + e.toString());

}

}
}



if you need more info let me know..

Viswa
IBM, Bangalore, INDIA
Mail : bgviswan@in.ibm.com
Web : http://viswa.itgo.com
--------

Mike Sandman

Posts: 6
Nickname: n8behavior
Registered: Feb, 2004

Re: search techniques in java Posted: Feb 17, 2004 8:17 PM
Reply to this message Reply
i would recommend using Lucene and "call it a day".

http://jakarta.apache.org/lucene/docs/index.html

keep in mind that this is an open source project i.e. a great place to "see how they did it"

:)

Flat View: This topic has 3 replies on 1 page
Topic: guys/gals hlp me plz Previous Topic   Next Topic Topic: How To Get The Correct Outcome

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use