Class LineIterator
java.lang.Object
org.miaixz.bus.core.center.iterator.ComputeIterator<String>
org.miaixz.bus.core.center.iterator.LineIterator
- All Implemented Interfaces:
Closeable,Serializable,AutoCloseable,Iterable<String>,Iterator<String>,IterableIterator<String>
public class LineIterator
extends ComputeIterator<String>
implements IterableIterator<String>, Closeable, Serializable
将Reader包装为一个按照行读取的Iterator 此对象遍历结束后,应关闭之,推荐使用方式:
LineIterator it = null;
try {
it = new LineIterator(reader);
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
it.close();
}
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionLineIterator(InputStream in, Charset charset) 构造LineIterator(Reader reader) 构造 -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()关闭Readerprotected String计算新的节点,通过实现此方法,当调用ComputeIterator.hasNext()时将此方法产生的节点缓存,直到调用ComputeIterator.next()取出 当无下一个节点时,须返回null表示遍历结束protected booleanisValidLine(String line) 重写此方法来判断是否每一行都被返回,默认全部为trueMethods inherited from class org.miaixz.bus.core.center.iterator.ComputeIterator
finish, hasNext, nextMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface org.miaixz.bus.core.center.iterator.IterableIterator
iteratorMethods inherited from interface java.util.Iterator
forEachRemaining, hasNext, next, remove
-
Constructor Details
-
LineIterator
构造- Parameters:
in-InputStreamcharset- 编码- Throws:
IllegalArgumentException- reader为null抛出此异常
-
LineIterator
构造- Parameters:
reader-Reader对象,不能为null- Throws:
IllegalArgumentException- reader为null抛出此异常
-
-
Method Details
-
computeNext
Description copied from class:ComputeIterator计算新的节点,通过实现此方法,当调用ComputeIterator.hasNext()时将此方法产生的节点缓存,直到调用ComputeIterator.next()取出 当无下一个节点时,须返回null表示遍历结束- Specified by:
computeNextin classComputeIterator<String>- Returns:
- 节点值
-
close
public void close()关闭Reader- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
isValidLine
重写此方法来判断是否每一行都被返回,默认全部为true- Parameters:
line- 需要验证的行- Returns:
- 是否通过验证
-