我必须为公司创建一个内部搜索引擎。客户端将有一个包含多个文本文件的文件夹。现在我的代码工作正常如果我将程序包含在与文本文件相同的文件夹中。反正我是否能够提示用户,以便他们可以给我文件的名称,然后使用它来搜索该文件夹中的文本文件的内容?以下是我到目前为止的情况。谢谢你的帮助!
import java.io.*;
import java.lang.Boolean;
public class SearchFunction{
public static void main (String[] args){
try{
/*create a count for each text file and a variable
*for the textfiles. */
int count = 1;
/* Wrap the File as a boolean so it can run in the loop*/
//boolean boolFileName = Boolean.parseBoolean(fileName);
do {
/*read the file and search for the keyword/phrase
*Loop through all the text files. */
String fileName = "Buyerbuyerserver" + count + "Log.txt";
BufferedReader br = new BufferedReader(new FileReader (fileName));
int linecount = 0;
String line;
System.out.println("Searching for '" + args[0] + "' in file " + fileName);
while ((line = br.readLine()) != null)
{
linecount++;
int indexfound = line.indexOf(args[0]);
if (indexfound > -1){
System.out.println("Word was found at positon " + indexfound + " on line " + linecount);
}
}
br.close();
count++;
} while (count > 0);
}
/*Catch if the file is not found*/
catch (IOException e){
System.out.println("IO Error Occurred: " + e.toString());
}
}
}
答案 0 :(得分:0)
如果我已正确理解您的问题,那么我希望这可以帮助您:
1)在代码的开头导入java.io. *,如下所示:
import java.io.*;
2)提示用户输入文件名:
System.out.println("Please Specify The File With Its Path: ");
Scanner s=new Scanner(System.in);
String filename=s.nextLine();
3)在前两个步骤之后继续使用您自己的代码