Java BufferedReader在循环之前检查循环的下一行
我正在分析一个.cvs文件。 对于CV的每一行,我使用解析后的值创建一个对象,并将它们放入一个集合中 在将对象放入贴图并循环到下一个之前,我需要检查下一个CV的线是否与实际的对象相同,但具有不同的特定属性值 为此,我需要检查缓冲区的下几行,但要将循环的缓冲区保持在相同的位置 例如:Java BufferedReader在循环之前检查循环的下一行,java,loops,bufferedreader,Java,Loops,Bufferedreader,我正在分析一个.cvs文件。 对于CV的每一行,我使用解析后的值创建一个对象,并将它们放入一个集合中 在将对象放入贴图并循环到下一个之前,我需要检查下一个CV的线是否与实际的对象相同,但具有不同的特定属性值 为此,我需要检查缓冲区的下几行,但要将循环的缓冲区保持在相同的位置 例如: BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file),"ISO-8859-1")); S
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file),"ISO-8859-1"));
String line = null;
while ((line = input.readLine()) != null) {
do something
while ((nextline = input.readLine()) != null) { //now I have to check the next lines
//I do something with the next lines. and then break.
}
do something else and continue the first loop.
}
BufferedReader.reset()
。标记的参数是“预读限制”;如果在读取超过限制后尝试重置(),则可能会出现IOException未读字符。但也有缺点:PushbackReader
不提供readLine
方法
考虑使用已经制作好的东西。我认为您可以为它创建类似的内容。您也可以使用嵌套的do…while循环来完成此操作,而无需重新读取流中的任何内容:
public void process(File file) throws Exception {
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
String batchParent = input.readLine(); // read the first line in the file, this is a new batch
String batchChild;
do {
currentBatch = new Batch(batchParent);
do {
batchChild = input.readLine();
} while (addToBatchIfKeysMatch(batchParent, batchChild));
// if we break out of the inner loop, that means batchChild is a new parent
// assign it to batchParent and continue the outer loop
batchParent = batchChild;
} while (batchParent != null);
}
private boolean addToBatchIfKeysMatch(final String batchParent, final String batchChild) {
if (batchChild != null && keysMatch(batchParent, batchChild)) {
currentBatch.add(batchChild);
return true;
} else {
return false;
}
}
关键是只读取每个文件并将其作为内部循环中的子行进行处理,或者将父行设置为新读取的值并继续外部循环。
BufferedReader()
有mark()
和reset()
方法可以帮助您(我从来没有使用过这些,所以我不把它们作为答案发布)。我可以简单地调用input.mark()而不传递任何参数吗?我如何使用input.mark()方法?我的意思是。我需要传递一个参数(我想这是我想要标记的位置),但我如何检索它,因为我正在读取行?它标记当前位置。无法检索它,您只能请求流返回到您之前标记的位置。该参数确定在标记无效之前您可以读取的距离。如果继续,您可能无法reset()
流。它有它的局限性,你必须接受它们,或者使用其他东西。
public void process(File file) throws Exception {
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
String batchParent = input.readLine(); // read the first line in the file, this is a new batch
String batchChild;
do {
currentBatch = new Batch(batchParent);
do {
batchChild = input.readLine();
} while (addToBatchIfKeysMatch(batchParent, batchChild));
// if we break out of the inner loop, that means batchChild is a new parent
// assign it to batchParent and continue the outer loop
batchParent = batchChild;
} while (batchParent != null);
}
private boolean addToBatchIfKeysMatch(final String batchParent, final String batchChild) {
if (batchChild != null && keysMatch(batchParent, batchChild)) {
currentBatch.add(batchChild);
return true;
} else {
return false;
}
}