Respuesta :
Answer:
public void search(String key) throws FileNotFoundException, IOException
{
try (BufferedReader br = new BufferedReader(new FileReader(infile)))
{
Recursion recursionObject=new Recursion();
int total_lines=1;
StringTokenizer st ;
String line;
System.out.println("\n-----------------\n");
while ((line = br.readLine()) != null)
{
st=new StringTokenizer(line);
if(recursionObject.search(st, key))
{
System.out.println(total_lines+" : "+line);
}
total_lines++;
}
}
}
Explanation:
- Create a function named search that takes key as a parameter.
- Read the file using the BufferedReader object inside the try block in case any exception occur.
- Initialize the necessary variables and run a while loop until the line variable is not equal to null.
- Check if the desired word is found and the display the results.
- Lastly increment the total_lines counter.