I have this code in JAVA:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TextFileSearch {
public static void main(String[] args) {
String filePath = "file.txt";
String searchTerm = "world";
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains(searchTerm)) {
System.out.println(line);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
And I want to make the code expandable, so that the file in the code will be loaded from a txt file in the application - according to the value entered for the file name in the application files, and also that the word to search for in the file will be a value received from the user in the extension.
What this code does: searches for a word in a text file, and if it finds the word, then it returns the word including the entire line where the link was found, for example if the file says:
what = yes
So if I search for “what” or “yes” then I get the whole above line.
It’s urgent for me, thanks.
Thanks!!