How to read a file as a byte array in Scala

fgysin reinstate Monica picture fgysin reinstate Monica · Sep 29, 2011 · Viewed 60.2k times · Source

I can find tons of examples but they seem to either rely mostly on Java libraries or just read characters/lines/etc.

I just want to read in some file and get a byte array with scala libraries - can someone help me with that?

Answer

Vladimir Matveev picture Vladimir Matveev · Jan 21, 2014

Java 7:

import java.nio.file.{Files, Paths}

val byteArray = Files.readAllBytes(Paths.get("/path/to/file"))

I believe this is the simplest way possible. Just leveraging existing tools here. NIO.2 is wonderful.