我试图从java中的文件中查找特定字符串的计数器,但是无法找到X_28的计数器。
我找到了X_29和我要为其寻找计数器的异常。
下面是代码
读取文件,直到找到正则表达式模式,然后转到上方的行 找到模式,并检查字符串X'28'。
如果找到,请增加计数器并检查X'29'的下一行。
如果找到了增量计数器X_29,否则打印异常为29,
其他打印异常为28。
public class ReadWrite {
public static void readFile() throws IOException, IndexOutOfBoundsException {
File file = new File("D:\\test001.yac");
BufferedReader br = new BufferedReader(new FileReader(file));
String pattern = "[N][\\(][0-9][0-9][\\)]";
Pattern ptr = Pattern.compile(pattern);
String st = "";
int line = 0;
int arr28[];
int arr29[];
ArrayList<String> ll = new ArrayList<String>();
// TODO loop for list of files
while ((st = br.readLine()) != null) {
ll.add(st);
}
for (int i = 0; i < ll.size(); i++) {
int X_28 = 0;
int X_29 = 0;
Matcher matcher = ptr.matcher(ll.get(i));
// if pattern found
if (matcher.find()) {
//if string contains
if (ll.get(i - 1).contains("X'28'")) {
X_28++;
System.out.println("Count28: " + X_28);
while (i < (ll.size() - 1) && !ll.get(i + 1).contains("X'29'")) {
matcher = ptr.matcher(ll.get(i));
if (matcher.find())
i++;
else {
//else part for string X'29' is working
System.out.println("Abnormality for 29::" + i);
X_29--;
break;
}
}
X_29++;
System.out.println("Count29: " + X_29);
System.out.println("----------------End--------------------");
}
//else part for string X'28' is not working please help
else {
//X_28 = 0;
//break;
System.out.println("Abnormality for 28:: " + (i + 1));
System.out.println("Count28: " + X_28);
}
}
}
}
public static void main(String[] args) throws Exception {
//static method call
readFile();
}
}