I just want to read file in resource and get a byte array? Can someone help me with that?
-
1Almost a duplicate of stackoverflow.com/questions/27360977/… (that one tries to read lines instead of bytes, but maybe you can adapt it, for example by combining it with stackoverflow.com/questions/7598135/…)Thilo– Thilo2015-11-17 11:19:10 +00:00Commented Nov 17, 2015 at 11:19
Add a comment
|
2 Answers
As described in How to read a file as a byte array in Scala, the following fragment should do the trick:
def slurp(resource: String) = {
val bis = new BufferedInputStream(getClass.getResource(resource))
try Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray
finally bis.close()
}
1 Comment
m.bemowski
This approach is very slow and heavy on CPU, because it reads the file byte by byte, one byte at a time.