E
elci via JavaKB.com
i am trying to write a program in java that searches a directory of files
for a string. i need to able to rank the documents in order of relevance.
at the moment i have managed to implement a program that can search a file
for a string. it will then print the line of occurrence, i need to be able
to seach a whole directory with subdirectories, i am having trouble with
this part, can anyone see in my code, below, why i can''t search a folder
of files, i can only search one file.
if i try to pass a directory i get an error, can anyone help me with this
please!!! sorry if i am asking a silly question, i am new to java and
unfortunately i am struggling.
for a string. i need to able to rank the documents in order of relevance.
at the moment i have managed to implement a program that can search a file
for a string. it will then print the line of occurrence, i need to be able
to seach a whole directory with subdirectories, i am having trouble with
this part, can anyone see in my code, below, why i can''t search a folder
of files, i can only search one file.
Code:
private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {
String filepath;
FileDialog fileDialog = new FileDialog(this, "Open...",
FileDialog.LOAD);
fileDialog.show();
if (fileDialog.getFile() == null)
return;
filepath = fileDialog.getDirectory() + File.separator +
fileDialog.getFile();
pathText.setText(filepath);
// TODO add your handling code here:
}
private void closeButtonMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void pathTextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void searchTextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
//String path = "NULL";
int rank = 0;
String text = searchText.getText();
String path = pathText.getText();
System.out.println(path);
boolean blnTitle = false;
boolean blnAuthor = false;
boolean blnAbstxt = false;
String title = "";
String author = "";
String abstxt = "";
int titleStart = text.indexOf(title);
int titleEnd = titleStart + title.length() - 1;
int abstractStart = text.indexOf(abstxt);
int abstractEnd = abstractStart + abstxt.length() - 1;
int authorStart = text.indexOf(author);
int authorEnd = authorStart + author.length() - 1;
try
{
//FileReader fileReader = new FileReader(inFile);
BufferedReader in = new BufferedReader(new FileReader(path));
String line = "";
while ((line = in.readLine())!=null)
{
if (line.indexOf(text)>= 0)
{
resultPane.append(line+"\n");
String delim = "@!#";
StringTokenizer tokenizer = new StringTokenizer
(line,delim,true);
while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
if (token.equals("@"))
{
blnTitle = true;
blnAbstxt = false;
blnAuthor = false;
resultPane.append("this was found in the title\
n");
}
if (token.equals("!"))
{
blnTitle = false;
blnAbstxt = true;
blnAuthor = false;
resultPane.append("this was found in the Body\
n");
}
if (token.equals("#"))
{
blnTitle = false;
blnAbstxt = false;
blnAuthor = true;
resultPane.append("this was found in the Author\
n");
}
if (blnTitle)
title += token;
if (blnAuthor)
author += token;
if (blnAbstxt)
abstxt += token;
}
//System.out.println(line);
rank++;
}
}
if (rank == 0)
resultPane.setText("word not found in file\n");
}
catch(IOException e)
{
System.out.println("error");
}
}
if i try to pass a directory i get an error, can anyone help me with this
please!!! sorry if i am asking a silly question, i am new to java and
unfortunately i am struggling.