
in.ashwanthkumar.utils.io.FileLineIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of my-java-utils Show documentation
Show all versions of my-java-utils Show documentation
My personal set of utils that I take along with my java projects.
The newest version!
package in.ashwanthkumar.utils.io;
import java.io.*;
import java.util.Iterator;
public class FileLineIterator implements Iterator, Iterable {
private BufferedReader reader;
private String line;
public FileLineIterator(File input) throws IOException {
reader = new BufferedReader(new FileReader(input));
init();
}
public FileLineIterator(InputStream input) throws IOException {
reader = new BufferedReader(new InputStreamReader(input));
init();
}
private void init() throws IOException {
line = reader.readLine();
}
@Override
public boolean hasNext() {
return line != null; // null if EOF is reached
}
@Override
public String next() {
String valueToReturn = line;
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return valueToReturn;
}
@Override
public void remove() {
throw new RuntimeException("FileLineIterator#remove is not implemented");
}
@Override
public Iterator iterator() {
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy