我有一个包含这样内容的文件
Fri Sep 19 19:16:39 UTC 2014
Test launched. Check every minute for an update
minute 9:
ec2-54-84-5-158.compute-1.amazonaws.com: Requests per second: 926.13 [#/sec] (mean)
----------------------
minute 10:
ec2-54-84-5-158.compute-1.amazonaws.com: Requests per second: 919.23 [#/sec] (mean)
----------------------
minute 11:
ec2-54-209-65-210.compute-1.amazonaws.com: Requests per second: 547.62 [#/sec] (mean)
ec2-54-84-5-158.compute-1.amazonaws.com: Requests per second: 875.14 [#/sec] (mean)
----------------------
minute 12:
ec2-54-209-65-210.compute-1.amazonaws.com: Requests per second: 862.83 [#/sec] (mean)
ec2-54-84-5-158.compute-1.amazonaws.com: Requests per second: 876.78 [#/sec] (mean)
----------------------
minute 13:
ec2-54-209-65-210.compute-1.amazonaws.com: Requests per second: 857.37 [#/sec] (mean)
ec2-54-84-5-158.compute-1.amazonaws.com: Requests per second: 880.64 [#/sec] (mean)
----------------------
它基本上每分钟都在不断更新。所以我想从这两个“------------”行之间的最后一个块中获取最后一个数据。最终目标是对该块的每秒请求数进行求和。你能指导我申请的最佳逻辑吗?
答案 0 :(得分:1)
此代码将识别每秒的请求数(字段#5),并将识别当前行上每秒请求数与包含每秒请求数的最后一行(最近一行)之间的差异。
此代码假定每秒的请求数始终显示在以“分钟”(没有引号)开头的行之后的行上。
try{
BufferedReader br = new BufferedReader(new FileReader("C:\\TEMP\\MINUTE.txt"));
String line;
String[] lineFields;
int i;
int thisLine_minute;
int lastLine_minute;
Double thisLine_reqpersec;
Double lastLine_reqpersec;
lastLine_minute = 0;
thisLine_reqpersec= 0.0;
lastLine_reqpersec= 0.0;
while ( (line = br.readLine()) != null) {
lineFields = line.split("\\s+");
System.out.printf("LINE READ: %s\n", line);
for (i=0; i < lineFields.length; i++) {
System.out.printf("FIELD: %d IS: %s\n", i, lineFields[i]);
}
if (lineFields[0].toUpperCase().equals("MINUTE")) {
thisLine_minute=1;
}
else {
thisLine_minute=0;
}
try {
if (lastLine_minute==1) {
lastLine_reqpersec = thisLine_reqpersec;
thisLine_reqpersec = new Double(lineFields[4]);
}
}
catch (Exception excepStrToInt) {
thisLine_reqpersec = 0.0;
}
System.out.printf("*** REQUESTS PER SECOND: %f\n",
thisLine_reqpersec);
System.out.printf("*** REQUESTS PER SECOND: DIFFERENCE: %f\n",
(thisLine_reqpersec- lastLine_reqpersec));
lastLine_minute=thisLine_minute;
}
br.close();
}
catch(Exception e){
}
答案 1 :(得分:0)
您可以执行split string
String yourBlocks[] = yourEntireFileInString.split("----------------------");
然后你想要的块
yourBlocks[yourBlocks.length-2]
更多您想要做的是
int numberRequest,numberRequest1;
String tempNumber;
for(String myString : yourBlocks) {
String [] splitedString = myString.split("Requests per second:");
tempNumber="";
for(int i = 0;i< splitedString[1].length ; i++) {
if(Charachter.isDigit()){
tempNumber+=splitedString[i];
}
if('#'==splitedString[i]){
break;
}
}
numberRequest = Integer.parseInt(tempNumber);
tempNumber="";
for(int i = 0;i< splitedString[3].length ; i++) {
if(Charachter.isDigit()){
tempNumber+=splitedString[i];
}
if('#'==splitedString[i]){
break;
}
}
numberRequest1 = Integer.parseInt(tempNumber);
// use numberRequest, numberRequest1 here
}
这可能效率不高,但会让你达到预期的目标。
答案 2 :(得分:0)
String result="";
BufferedReader bf = new BufferedReader(new FileReader("your_file_path"));
int dashCount=0;
String finalResult= "";
while ((result = bf.readLine()) != null) {
if (result.length() != 0) {
if (result.charAt(0) == ’-’) {
dashCount=dashCount+1;
if(dashCount==2) break;
}
finalResult+=result;
}
}
答案 3 :(得分:0)
所以你试图用虚线对每个块内的值求和,对吧?而不是试图将文件分开,只需逐行阅读,直到你到达文件的末尾,并对你遇到的任何一行作出适当的反应:
如果您希望使用所有每秒的总和做一些事情(比如平均值),那么每次读完一个块时都要做你需要做的事情。如果您关心的是最后一个块,那么只需保存每次计算的最后一个总和,覆盖最后一个值;当你点击文件中的最后一个虚线时,你已经计算并保存了最后一个块的总和,并且它会在那里供你阅读并用它做你需要的。但是我的直觉说你想要使用所有的价值观,而不仅仅是最后一个......