Created
April 24, 2017 16:57
-
-
Save randomstatistic/2d57608600038d0b723db88b7a7cf02c to your computer and use it in GitHub Desktop.
Resource file reader
This file contains hidden or 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
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