5

I just want to read file in resource and get a byte array? Can someone help me with that?

1

2 Answers 2

3

Note: this requires Java 9+

I use the following to read a file in my resources as byte array:

getClass.getResourceAsStream("/file-in-resource-folder").readAllBytes()
Sign up to request clarification or add additional context in comments.

Comments

2

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

This approach is very slow and heavy on CPU, because it reads the file byte by byte, one byte at a time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.