Skip to content

Instantly share code, notes, and snippets.

@dirkgr
Created November 15, 2018 23:29
Show Gist options
  • Save dirkgr/c2c0846bcd7714423a7e6634257fdd89 to your computer and use it in GitHub Desktop.
Save dirkgr/c2c0846bcd7714423a7e6634257fdd89 to your computer and use it in GitHub Desktop.
Iterator of lines of text from file
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