How to read a file as a byte array in Scala

ScalaIoBytearray

Scala Problem Overview


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?

Scala Solutions


Solution 1 - Scala

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.

Solution 2 - Scala

This should work (Scala 2.8):

val bis = new BufferedInputStream(new FileInputStream(fileName))
val bArray = Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray

Solution 3 - Scala

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.

Solution 4 - Scala

val is = new FileInputStream(fileName)
val cnt = is.available
val bytes = Array.ofDim[Byte](cnt)
is.read(bytes)
is.close()

Solution 5 - Scala

You might also consider using scalax.io:

scalax.io.Resource.fromFile(fileName).byteArray

Solution 6 - Scala

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))

Solution 7 - Scala

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)
      }
    }
  }

Solution 8 - Scala

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() 
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfgysinView Question on Stackoverflow
Solution 1 - ScalaVladimir MatveevView Answer on Stackoverflow
Solution 2 - ScalaJus12View Answer on Stackoverflow
Solution 3 - Scalafengyun liuView Answer on Stackoverflow
Solution 4 - ScalareivzyView Answer on Stackoverflow
Solution 5 - ScalaOlivierBlanvillainView Answer on Stackoverflow
Solution 6 - ScalaSagiView Answer on Stackoverflow
Solution 7 - ScalaAntonio Maria Sanchez BerrocalView Answer on Stackoverflow
Solution 8 - ScalaKareekView Answer on Stackoverflow