我正在解析 .csv 文件。
对于 cvs 的每一行,我使用解析的值创建一个对象,并将它们放入一个集合中。
在将对象放入地图并循环到下一个对象之前,我需要检查下一个 cvs 行是否与实际对象相同,但特定属性值不同。
为此,我需要检查缓冲区的下一行,但将循环缓冲区保持在同一位置。
例如:
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.mark(int) http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#mark%28int%29。返回到您调用的位置BufferedReader.reset()
。参数为mark
是“预读限制”;如果你尝试reset()
读取超过限制后,您可能会收到 IOException。
-
或者你可以使用RandomAccessFile http://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html反而:
// Get current position
long pos = raf.getFilePointer();
// read more lines...
// Return to old position
raf.seek(pos);
或者你可以使用PushbackReader http://docs.oracle.com/javase/7/docs/api/java/io/PushBackReader.html这可以让你unread
人物。但有一个缺点:PushbackReader
不提供readLine
method.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)