demonstrates a bug of Scala standard library
Latest commit 38744a2 Jan 21, 2014 @liufengyun Update
Failed to load latest commit information. Update Jan 21, 2014
data.bin initial commit Jan 21, 2014
main.scala initial commit Jan 21, 2014


This program demonstrates a bug of Scala standard library in reading binary files.


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:


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

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

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


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

How to run

Just run scala main.scala.

How to solve the problem

Well, there seems to be no good library in Scala to read binary files. However, there's a way to make the example above work by setting encoding of the file to ISO-8859-1 as follows:

val ss = Source.fromFile("data.bin", "ISO-8859-1")

It looks strange to set encoding for reading binary files, but is designed for text files. It's a pity that Scala doesn't provide a convenient library for reading binary files.


You can use hexdump data.bin or xxd data.bin to view the contents of data.bin.