我想读取数据流,每次读取某个单词或短语时我都希望计数增加。我下面的例子无法计算它。我也尝试寻找“回声百分比”。 bat 文件所做的只是回显百分比。
try {
String ls_str;
String percent = "percent";
Process ls_proc = Runtime.getRuntime().exec("c:\\temp\\percenttest.bat");
// get its output (your input) stream
DataInputStream ls_in = new DataInputStream(ls_proc.getInputStream());
while ((ls_str = ls_in.readLine()) != null ) {
System.out.println(ls_str);
progressBar.setValue(progress);
taskOutput.append(String.format(ls_str+"\n", progress));
if (ls_str == percent) {
progress++;
}
}
} catch (IOException e1) {
System.out.println(e1.toString());
e1.printStackTrace();
}
setProgress(Math.min(progress, 100));
最佳答案
DataInputStream.readLine
已弃用。使用BufferedReader
及其 readLine
方法或Scanner
和 nextLine
反而。另外,使用 .equals
来比较两个字符串,而不是 ==
。
==
比较仅进行引用比较,提出问题“这两个字符串在内存中的同一位置吗?”通常,答案是“不”。另一方面,equals
提出了一个问题:“这两个字符串中的字符相同吗?”这称为深度比较,==
运算符不执行深度比较。
关于java - 读取输入流并使用 if 语句比较输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12284413/