Here is a simple example :
public static void main(String args[]) {
try {
List<String> lines = Files.readAllLines(new File("path\\file.txt").toPath());
List<String> result = new ArrayList<>();
String key = "Thu May 16 08:27:25";
boolean start = false;
for (String line : lines) {
if (start) {
result.add(line);
}
if (line.contains(key)) {
start = true;
}
}
for (String line : result) {
System.out.println(line);
}
} catch (IOException ex) {
//throw exception
}
}
Output
Fri May 17 08:27:25 EST 2013 few words
Sat May 18 08:27:25 EST 2013 few words
Sun May 19 08:27:25 EST 2013 few words
The idea is :
- Get all the lines of your files
List<String> lines = Files.readAllLines(file.toPath());
- Loop throw your lines, and check if the line contain the key word
s.contains(key)
you want to check, start to fill your result List
For better performance and if your file contain a lot of lines, you can loop throw your file just one time, and check you can use also this solution :
public static void main(String args[]) {
try {
FileInputStream fstream = new FileInputStream("path\\file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String lines;
String key = "Thu May 16 08:27:25";
boolean start = false;
List<String> result = new ArrayList<>();
while ((lines = br.readLine()) != null) {
if (start) {
result.add(lines);
}
if (lines.contains(key)) {
start = true;
}
}
for (String line : result) {
System.out.println(line);
}
} catch (IOException ex) {
//throw exception
}
}