我具有以下目录结构。
- main
-- scala
-- resources
--- file.json
我想将file.json
读取为java.io.InputStream 。我知道我可以将其读为Source
,但不知道如何获得InputStream
。
答案 0 :(得分:2)
我使用以下方式。
getClass.getClassLoader.getResourceAsStream(resourceName)
答案 1 :(得分:0)
您在我写答案时回答了:
我提出此解决方案:
def readJsonWithTry(filePath: String): Try[InputStream] = {
Try {
val lines = getClass.getClassLoader.getResourceAsStream(filePath)
lines
}
}
readJsonWithTry("resourceName") match {
case Success(s) => s
case Failure(f) => println(s"Failed, message is: $f")
}