Package com.luna.common.io
Class LineIter
java.lang.Object
com.luna.common.io.LineIter
- All Implemented Interfaces:
Closeable
,Serializable
,AutoCloseable
,Iterable<String>
,Iterator<String>
public class LineIter
extends Object
implements Iterator<String>, Iterable<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(); }此类来自于Apache Commons io
- Since:
- 4.1.1
- Author:
- looly
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
LineIter
构造- Parameters:
in
-InputStream
charset
- 编码- Throws:
IllegalArgumentException
- reader为null抛出此异常
-
LineIter
构造- Parameters:
reader
-Reader
对象,不能为null- Throws:
IllegalArgumentException
- reader为null抛出此异常
-
-
Method Details
-
hasNext
public boolean hasNext() -
next
返回下一行内容- Specified by:
next
in interfaceIterator<String>
- Returns:
- 下一行内容
- Throws:
NoSuchElementException
- 没有新行
-
nextLine
返回下一行- Returns:
- 下一行
- Throws:
NoSuchElementException
- 没有更多行
-
close
public void close()关闭Reader- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
remove
public void remove()不支持移除- Specified by:
remove
in interfaceIterator<String>
- Throws:
UnsupportedOperationException
- 始终抛出此异常
-
isValidLine
重写此方法来判断是否每一行都被返回,默认全部为true- Parameters:
line
- 需要验证的行- Returns:
- 是否通过验证
-
iterator
-