Skip to content

Instantly share code, notes, and snippets.

@randomstatistic
Created April 24, 2017 16:57
Show Gist options
  • Save randomstatistic/2d57608600038d0b723db88b7a7cf02c to your computer and use it in GitHub Desktop.
Save randomstatistic/2d57608600038d0b723db88b7a7cf02c to your computer and use it in GitHub Desktop.
Resource file reader
import java.io.FileNotFoundException
import java.util.zip.GZIPInputStream
import scala.io.Codec
object ClasspathReader {
def getResourceAsStream(filename: String) = {
// why doesn't the more scala-like getClass.getResourceAsStream work?
val inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)
if (inputStream == null) throw new FileNotFoundException(s"Couldn't find $filename on the classpath")
inputStream
}
def getGzipResourceAsStream(filename: String) = new GZIPInputStream(getResourceAsStream(filename))
def getLines(filename: String) = {
val inputStream =
if (filename.matches(""".*\.gz"""))
getGzipResourceAsStream(filename)
else
getResourceAsStream(filename)
io.Source.fromInputStream(inputStream)(Codec.UTF8).getLines()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment