简体   繁体   中英

Get the line of last occurrence of a particular string in a text file in java

I want the line of last occurrence of a particular string in a text file

Scanner file = new Scanner(new File("C:\\Users\\prudra\\Desktop\\Axalta\\20180406114505\\activemq.log_app1"));
int i = 0;
String str = file.nextLine();
System.out.print("Enter search ");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the environment you want to execute the script (Dev/QA/Prod)");
String name = sc.nextLine();
while (file.hasNextLine()) {
 final String lineFromFile = file.nextLine();
 i++;
 if (lineFromFile.contains(name)) {
  //System.out.println("file.txt");
 }
}
System.out.println(i);
}

If I understand correctly you wanted something like this?

int lastIndex= 0;
//start of loop
if (lineFromFile.contains(name)) {
  lastIndex = i;
}

//end of loop

if (lastIndex != 0) {
    System.out.println(lastIndex);
} else {
    System.out.println("Line not found");
}

Or maybe only increment the i in condition

int i = 0;
//start of loop
if (lineFromFile.contains(name)) {
  i++;
}

//end of loop

if (i != 0) {
    System.out.println(i + 1); //because you start i from 0
} else {
    System.out.println("Line not found");
}
Scanner file = new Scanner(new File("C:\\Users\\prudra\\Desktop\\Axalta\\20180406114505\\activemq.log_app1"));
int i = 0;
int index = 0;
String str = file.nextLine();
System.out.print("Enter search ");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the environment you want to execute the script (Dev/QA/Prod)");
String name = sc.nextLine();
while (file.hasNextLine()) {
 final String lineFromFile = file.nextLine();
 i++;
 if (lineFromFile.contains(name)) {
  //saves index of occurance
  index = i;
 }
}
System.out.println(index);
}

I guess this is what you're looking for. Just need a variable to hold the line of the last occurrence found. Since you just want the last, after reading all lines index will hold the last one. In case you want the first you can return i after finding the first one. If you need x occurrence you would need to keep a count in another variable.

EDIT: Not sure if by line you mean line number of line text, if it's the text, instead of using a index to hold the line number, you can use a string to hold the line text like lineText = lineFromFile;

Store the last line in a local variable and access it out-of the loop.

Example:

 Scanner file = new Scanner(new File("C:\\Users\\prudra\\Desktop\\Axalta\\20180406114505\\activemq.log_app1"));
int i = 0;
String str = file.nextLine();
System.out.print("Enter search ");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the environment you want to execute the script (Dev/QA/Prod)");
String name = sc.nextLine();
String lastLine="";
int lastLineNumber=-1;
while (file.hasNextLine()) {
 final String lineFromFile = file.nextLine();
 i++;
 if (lineFromFile.contains(name)) {
   lastLine=lineFromFile;
   lastLineNumber=i;
  //System.out.println("file.txt");
 }
}
System.out.println(i);
System.out.println("Last Line: "+lastLine);
System.out.println("Last Line Number: "+lastLineNumber);
}

This actually works as long as the file pointer starts reading from the first-line through the last-line of the file. Since at the end of the loop you are storing the last occurrence of the line of text you want, then you can access it later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM