Logo 
Search:

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: 766
  

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.
res.setContentType("text/html");

//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("<HTML>");
out.println("<HEAD><TITLE>Search </TITLE></HEAD>");
out.println("<BODY>");

//Register the JDBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//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 =
rs.getMetaData();
int numColumns = resultSetMetaData.getColumnCount();
path = new String[numColumns];

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

conn.close();
}
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]
+"
</h2><br>");
}
else
out.println("<h2>The word you search for "+search
+"
was NOT found in the file "+path[i]
+"
</h2><br>");
}

}
/* 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";
else
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;
else
s2="no";
fr.close();
in.close();
}
if (s2 == "yes") return "yes";
else
return "no";

}
}
///////////////////////////////////////////////////
what is the wrong???

Share: 

 

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.




Tagged: