I am searching in a bigger txt file (~8Mb) where every row looks like this: dd/mm/yyyy hh:mm:ss after these there are different identifiers and after that a value.
dd/mm/yyyy hh:mm:ss identifier not relevant in search
dd/mm/yyyy hh:mm:ss identifier not relevant in search
dd/mm/yyyy hh:mm:ss identifier not relevant in search
dd/mm/yyyy hh:mm:ss identifier value -> value that I am searching for
dd/mm/yyyy hh:mm:ss identifier not relevant in search
dd/mm/yyyy hh:mm:ss identifier not relevant in search
I have successfully found and print out the whole line which contains the value that I am searching for, but I need to print the values around it(9 line after, 5 lines before), in the other lines.
public void fajl(String fajlnev, String szeriaszam) throws FileNotFoundException{
int sorszam = 0;
Scanner serialnummer = new Scanner(new File(fajlnev));
while(serialnummer.hasNext()){ //keresés pörgetése
String line = serialnummer.nextLine().toString();
sorszam++;
if(line.contains(szeriaszam)){
System.out.println(line + "\n" + sorszam);
The searching and the printing out looks like this. As you can see I can also tell the line number but I have no idea how to print out the other lines. I was thinking about uploading an array or something like this but don't know where to start.