我的代码基本上看起来像这样:
bufferedReader = new BufferedReader(new InputStreamReader(inputFile));
while ((currentLine = bufferedReader.readLine()) != null) {
Document document;
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
document = dBuilder.parse(new InputSource(new StringReader(currentLine)));
document.getDocumentElement().normalize();
NodeList nList = document.getElementsByTagName("record");
int j = 0;
int k = 0;
while (j < lines) {
if (currentLine.contains(bList.get(k))) {
myList.add(currentLine);
bufferedReader.mark(0);
k++;
j++;
}
else if (myList.size() == bList.size()) {
break;
}
else {
j++;
}
}
我有一个bufferedReader,它逐行通过一个文件。我想要的是根据行是否包含bList中存在的字符串,将单行添加到myList中。无论出于何种原因,当我调试时,尽管k ++递增,bList都不会移至下一个索引。我也不确定在这里使用bufferedReader.mark(0)是否正确,我以前使用过inputFile.getChannel()。position(0);但是我遇到了:
'错误:XML文档结构必须在同一实体内开始和结束'
我在调试时注意到的另一个问题是,当myList和bList的大小相等时,while循环不会中断。相反,myList一直保持大小为34,而bList保持为2。
编辑:添加了更多行以便于上下文和理解
如果当前行不包含bList @索引k中的任何内容,则k永远不会递增,因此将停留在该单词上。
如果currentline包含bList else if (myList.size() == bList.size())
中的内容,则不会执行,并且您不想使用break,这只是不好的样式。而是去while(... && myList.size() < bList.size()
)
此外,我不理解多次将同一行添加到myList的逻辑,您可能想遍历其他行。
bufferedReader.mark(0);
的目的是什么?