84

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?

9
  • 3
    I think relying on Java libraries is what (almost?) everyone would do, the Scala library included. See for instance the source code of scala.io.Source.
    – Philippe
    Sep 29, 2011 at 13:44
  • 2
    You're not using a different language, just a standard JVM API that has proved good enough not to need replacing! Sep 29, 2011 at 14:12
  • 4
    Well, how do you think the Java classes are implemented? Deep down, somewhere, there is a native method: it has just a signature, no Java implementation, and relies on an OS-specific C implementation. Isn't that cheating too? :)
    – Philippe
    Sep 29, 2011 at 14:47
  • 2
    It should be said that Scala on .Net does make this a more pressing issue. Sep 29, 2011 at 20:19
  • 4
    @Philippe: Sure, and using C is only cheating on assembly :P... What I meant is just, that the border between languages is usually rather clearly defined, Scala and Java sort of melt into each other.
    – fgysin
    Sep 30, 2011 at 9:59

8 Answers 8

143

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.

1
  • 1
    I think that anyone not bound to jvm < 7 should use this.
    – fedesilva
    Nov 24, 2015 at 17:51
47

This should work (Scala 2.8):

val bis = new BufferedInputStream(new FileInputStream(fileName))
val bArray = Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray
6
  • I think this is a great example of wrapping a Java API function to get Stream semantics. Much appreciated.
    – qu1j0t3
    Oct 28, 2012 at 22:37
  • 3
    val bis = new java.io.BufferedInputStream(new java.io.FileInputStream(fileName)); if you do not have the java paths imported
    – BeniBela
    Sep 21, 2013 at 16:29
  • 1
    Using this approach, is closing the file also needed or is it implicit?
    – Max
    Nov 20, 2013 at 0:18
  • 1
    You need to close it yourself
    – Tony K.
    Apr 1, 2014 at 23:41
  • 15
    This approach is slow, since it needs to process each and every byte. Ideally, I/O operations should be block-based.
    – Dibbeke
    Aug 31, 2014 at 17:10
6

The library scala.io.Source is problematic, DON'T USE IT in reading binary files.

The error can be reproduced as instructed here: https://github.com/liufengyun/scala-bug

In the file data.bin, it contains the hexidecimal 0xea, which is 11101010 in binary and should be converted to 234 in decimal.

The main.scala file contain two ways to read the file:

import scala.io._
import java.io._

object Main {
  def main(args: Array[String]) {
    val ss = Source.fromFile("data.bin")
    println("Scala:" + ss.next.toInt)
    ss.close

    val bis = new BufferedInputStream(new FileInputStream("data.bin"))
    println("Java:" + bis.read)
    bis.close
  }
}

When I run scala main.scala, the program outputs follows:

Scala:205
Java:234

The Java library generates correct output, while the Scala library not.

2
  • 11
    If I set the encoding to Source.fromFile("data.bin", "ISO8859-1"), it works well. Jan 21, 2014 at 15:57
  • 6
    Maybe it's helpful, but really, this isn't an answer. Introducing a new problem in an answer is not constructive and belongs somewhere else.
    – Benjamin
    May 18, 2017 at 5:14
5
val is = new FileInputStream(fileName)
val cnt = is.available
val bytes = Array.ofDim[Byte](cnt)
is.read(bytes)
is.close()
1
  • 3
    It is not a valid solution. From javadoc of InputStream.available: Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
    – m.bemowski
    Sep 27, 2018 at 14:22
4

You might also consider using scalax.io:

scalax.io.Resource.fromFile(fileName).byteArray
1
  • 5
    Noticed that the last actions on that repository are 6 years ago - is it still relevant?
    – akauppi
    Nov 20, 2014 at 12:08
2

You can use the Apache Commons Compress IOUtils

import org.apache.commons.compress.utils.IOUtils

val file = new File("data.bin")
IOUtils.toByteArray(new FileInputStream(file))
1
  • 1
    I had to import import org.apache.commons.io.IOUtils instead of the suggested import.
    – niid
    Feb 27, 2020 at 7:54
0

Asynchronous File reading using Scala Future and Java NIO2

  def readFile(path: Path)(implicit ec: ExecutionContext): Future[Array[Byte]] = {
    val p = Promise[Array[Byte]]()
    try {
      val channel = AsynchronousFileChannel.open(path, StandardOpenOption.READ)
      val buffer = ByteBuffer.allocate(channel.size().toInt);
      channel.read(buffer, 0L, buffer, onComplete(channel, p))
    }
    catch {
      case t: Exception => p.failure(t)
    }
    p.future
  }

  private def onComplete(channel: AsynchronousFileChannel, p: Promise[Array[Byte]]) = {
    new CompletionHandler[Integer, ByteBuffer]() {
      def completed(res: Integer, buffer: ByteBuffer): Unit = {
        p.complete(Try {
          buffer.array()
        })
      }

      def failed(t: Throwable, buffer: ByteBuffer): Unit = {
        p.failure(t)
      }
    }
  }
-2

I have used below code to read a CSV file.

import scala.io.StdIn.readLine
import scala.io.Source.fromFile

readFile("C:/users/xxxx/Downloads/", "39025968_ccccc_1009.csv")

def readFile(loc :String,filenm :String): Unit ={

  var flnm = fromFile(s"$loc$filenm") // Imported fromFile package

  println("Files testing")
  /*for (line <- flnm.getLines()) {
    printf("%4d %s\n", line.length, line)
  }*/
  flnm.getLines().foreach(println) // getLines() is imported from readLines.
  flnm.close() 
}
2
  • 4
    With a question this old (asked over 9 years ago), and with so many answers already submitted, it is helpful to point out how your new answer is different from the previous answers. (And including code that's been commented out just looks sloppy.)
    – jwvh
    Oct 26, 2020 at 23:54
  • yeah.. the other answers clearly show a byte array being returned. this is really not clear Feb 5, 2021 at 9:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.