我正在编写的文本解析器发生了一些奇怪的事情,我想也许你们这些专家可以发现我做错了什么。
解析器正在文本中搜索多个搜索词,并将找到的结果复制到 SD 卡上的输出解析文件中。
代码是:
String line; // line reading buffer
…
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
…
while ((line = bufferedReader.readLine()) != null){
line = bufferedReader.readLine(); // reading the buffer
/*** parsing the buffer line by line ***/
if (line.contains("SearchTermA")){
// parsing the text in this line
}
if (line.contains("SearchTermB")){
// parsing the text in this line
}
// Check that we got to a cretin part in the file
if (line.contains("SearchTerm_text")){
textID = 1;
}
if (textID == 1){
if (line.contains("SearchTermC")){
// parsing the text in this line
}
}
现在,问题是在文件的开头(文件很长)这工作正常,但有时 SearchTermB 会出现在原始文本中,但不会被代码欺骗。我尝试在目标 android 机器上使用 Eclipse 单步执行代码,我可以清楚地看到“行”包含 SerchTermB 但调试器忽略此 IF 语句并继续执行下一个 IF 语句。
可能是 line.containes() 缺少搜索词?
请帮助找出我做错了什么,因为这让我晚上无法休眠......
谢谢,
最佳答案
您的 while
循环一次读取两行...
while ((line = bufferedReader.readLine()) != null){
line = bufferedReader.readLine(); // reading the buffer
换句话说,您在 while
语句中调用 readLine()
以检查它不是一个 null
结果,然后您立即再次调用 readLine()
。
摆脱这个...
line = bufferedReader.readLine(); // reading the buffer
...看看会发生什么。
关于java - 可能是 line.containes() 在 Android 中缺少搜索词吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8831610/