我如何阅读此条件并计算我的值?
我有一个动态大文件,我想检查一个布尔值,然后如果它是真的,请计数一个东西,
请帮助我
,例如这是我的文件(它可能有 20000 行)
.
.
.
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
.
.
.
我编写了这段代码来处理它,但我可以不是因为在这种情况下,我逐行阅读,我必须检查 value1 是否为真,然后在 2 行中,我必须计算 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);
}
我如何检查条件,然后如果为真,我计算 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想遍历该文件,只需检查每一行并检查其
myval
计数以及是否有value1 = true
。您将知道myval
的计数是多少,但除非value1
为 true,否则您不必报告它。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 hasvalue1 = true
. You'll know what the count ofmyval
is, but you won't have to report it unlessvalue1
is true.尝试:
Try: