- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringInTextFile.java
More file actions
Latest commit
22 lines (19 loc) · 709 Bytes
/
Copy pathStringInTextFile.java
File metadata and controls
22 lines (19 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
importjava.io.*;
publicclassStringInTextFile {
publicstaticvoidmain(String[] args) {
StringfilePath = "path_to_your_file.txt";
StringsearchString = "your_search_string";
try (BufferedReaderbr = newBufferedReader(newFileReader(filePath))) {
Stringline;
intlineNumber = 1;
while ((line = br.readLine()) != null) {
if (line.contains(searchString)) {
System.out.println("Line " + lineNumber + ": " + line);
}
lineNumber++;
}
} catch (IOExceptione) {
System.out.println("An error occurred.");
}
}
}