How can I read this Condition and count myvals?
I have a dynamic big File and I want check a Boolean value and then if it's true, get count of a thing
please help me
for example this is my file (it maybe has 20000 lines)
.
.
.
fsfds fdsfsdf gfhgfh value1=true fdsfd fdfdsr ewref
dfdfsd dsfds dfdf fdsfsd
dfdfsd dsfds myval dfdf fdsfsd
dfdfsd dsfds dfdf fdsfsd
dfdfsd dsfds myval dfdf fdsfsd
fdfdfddsfds
fdfdsfdfdsfdfdsfsdfdsfdsfdsfds
.
.
.
I wrote this code to handle it but I can't because in this case I read Line by Line and I must check if value1 is true then in 2 line after I must count myval ...
public void countMethod() {
int i = 0; //count
try {
开发者_开发知识库 BufferedReader input =
new BufferedReader(new FileReader(new File("I:\\1.txt")));
String line;
while ((line = input.readLine()) != null) {
if (line.substring(line.indexOf("value1")).split("=")[1].equals("true")) {
if (line.indexOf("myval") != -1)
i++;
}
}
input.close();
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Count is : " + i);
}
How can I check condition and after that if it's true I count myvals?
Thanks a lot
If you only want to make one pass through the file, just inspect each line and check both its count of myval
and whether it has value1 = true
. You'll know what the count of myval
is, but you won't have to report it unless value1
is true.
Try:
boolean found = false;
while ((line = input.readLine()) != null) {
if (!found)
found=line.substring(line.indexOf("value1")).split("=")[1].equals("true");
if (found && line.indexOf("myval") != -1)
i++;
}
精彩评论