I require searching a word in a text file and display the line number using java. If it appears more than once I need to show all the line numbers in the output. Can anyone
Something like this might work:
public ArrayList find(String word, File text) throws IOException {
LineNumberReader rdr = new LineNumberReader(new FileReader(text));
ArrayList results = new ArrayList();
try {
String line = rdr.readLine();
if (line.indexOf(word) >= 0) {
results.add(rdr.getLineNumber());
}
} finally {
rdr.close();
}
return results;
}