Created
November 15, 2018 23:29
-
-
Save dirkgr/c2c0846bcd7714423a7e6634257fdd89 to your computer and use it in GitHub Desktop.
Iterator of lines of text from file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def linesFromFile(filename: String): Iterator[String] = new Iterator[String] { | |
val bufferedReader = { | |
val fileInputStream = new FileInputStream(filename) | |
val decompressedInputStream = | |
if(filename.endsWith(".gz")) new GZIPInputStream(fileInputStream) else fileInputStream | |
val reader = new InputStreamReader(decompressedInputStream, "UTF-8") | |
new BufferedReader(reader) | |
} | |
private var nextLine = bufferedReader.readLine() | |
override def hasNext: Boolean = nextLine != null | |
override def next(): String = { | |
if(nextLine == null) throw new NoSuchElementException() | |
val result = nextLine | |
nextLine = bufferedReader.readLine() | |
if(nextLine == null) bufferedReader.close() | |
result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment