Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Problem in Reading a File (Please replay quickly)

  Asked By: Erica    Date: Mar 23    Category: Java    Views: 650

i wrot the follwing code to search for a word in a list of files
when i compile it ,it does not give me any error , but at run it give
me the following Eception:

java.io.FileNotFoundException: home.html (The system cannot find the
file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileReader.(Unknown Source)
at webSearchServlet.readFile(webSearchServlet.java:111)
at webSearchServlet.doGet(webSearchServlet.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at desisoft.server.servlets.Servlet.service(Servlet.java:86)
at desisoft.server.servlets.Servlet.doService(Servlet.java:62)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.String.*;

public class webSearchServlet extends HttpServlet{
String[] path;
PrintWriter out;
String search ;
int num = 0;
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException{

try {
Statement stmt;//used for database access
ResultSet rs;//used for database access
Connection conn;

//Set the content type of the data to be sent back
// to the client.

//Get an output writer object to send data back to
// the client.
out= res.getWriter();

//Begin constructing the HTML page to send back to
// the client.
out.println("<HEAD><TITLE>Search </TITLE></HEAD>");

//Register the JDBC driver
//Construct URL for database on node3
String url = "jdbc:odbc:search";
String username="";
String password="";

//Get a connection to the database
conn = DriverManager.getConnection(url,username,password);
//Get a Statement object
stmt = conn.createStatement();
search = req.getParameter("search");
//Create a statement object linked to the database
// connection.
stmt = conn.createStatement();

//Use the statement object to execute a query on the
// database.
String query = "SELECT Path FROM searchTable";
rs = stmt.executeQuery(query);
ResultSetMetaData resultSetMetaData =
int numColumns = resultSetMetaData.getColumnCount();
path = new String[numColumns];

while (rs.next()) {
path[num] = rs.getString("Path");
String[] result = searchString(search ,num);

catch(Exception e){e.printStackTrace();}
for (int i=0 ; i<path.length ;i++){
String output = readFile(path[i] ,search);
if(output == "yes"){
out.println("<h2>The word you search for "+search
was found in the file "+path[i]
out.println("<h2>The word you search for "+search
was NOT found in the file "+path[i]

/* a method to test the quary string of the user
*it tack a String value and return a String array.
public String[] searchString(String str , int numberOfFiles) {
String[] strArray =new String[numberOfFiles];
int indexOfPlus = str.indexOf('+');
if (indexOfPlus == -1) {
strArray[0] = str;
return strArray;
else {
String firstWord = str.substring(0 ,indexOfPlus-1);
String secondWord = str.substring(indexOfPlus-1);
strArray[0] = firstWord;
strArray[1] = secondWord;
return strArray;


public String searchFile(String stringLine ,String word){
int index = stringLine.indexOf(word);
if (index == -1) return "no";
return "yes";

public String readFile (String fileName ,String word) throws
IOException ,RuntimeException ,NullPointerException
,FileNotFoundException {
//Read input by lines
FileReader fr = new FileReader(fileName);
BufferedReader in = new BufferedReader (fr);
String s1,s2 = new String();
while ((s1 = in.readLine()) != null ) {
s2 = searchFile(s1,word);
if (s2 == "yes") break;
if (s2 == "yes") return "yes";
return "no";

what is the wrong???



No Answers Found. Be the First, To Post Answer.

Didn't find what you were looking for? Find more on Problem in Reading a File (Please replay quickly) Or get search suggestion and latest updates.