简体   繁体   English

在java中获取文本文件中特定字符串最后一次出现的行

[英]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或者也许只在条件中增加 i

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.由于您只想要最后一个,在阅读所有行后, index将保存最后一个。 In case you want the first you can return i after finding the first one.如果你想要第一个,你可以在找到第一个后返回i If you need x occurrence you would need to keep a count in another variable.如果您需要 x 出现,则需要在另一个变量中保留一个计数。

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;编辑:不确定是否按行表示行文本的行号,如果是文本,而不是使用索引来保存行号,您可以使用字符串来保存行文本,如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.由于在循环结束时您存储的是您想要的文本行的最后一次出现,因此您可以稍后访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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