Scala code to print the byte values of text files

This is some test code I wrote. It shows how to read a text file in Scala.

This first program shows how to read the entire file into memory and print out the “byte values” of each byte/character in the file:

import scala.io.Source
import scala.util.control.Breaks._

object FileAsBytes extends App {

    // get the (entire) file contents
    val filename = "Chapter5.md"
    val bufferedSource = Source.fromFile(filename)
    val text = bufferedSource.mkString
    bufferedSource.close

    breakable {
        for ((char, count) <- text.zipWithIndex) {
            println(s"$char => ${char.toByte}")
            if (count > 250) break
        }
    }

}

This second example shows the same thing, how to show the byte value of each byte in a file using Scala, but it uses an iterator, so it should be able to read/parse a file of any size:

import scala.io.Source
import scala.util.control.Breaks._

object Iterator extends App {

    val filename = "Chapter5.md"
    val bufferedSource = Source.fromFile(filename)

    breakable {
        var count = 0
        bufferedSource.foreach { char =>
            count += 1
            println(s"$char => ${char.toByte}")
            if (count > 250) break
        }
    }

    bufferedSource.close

}

I wrote the first program because I wanted a way to see the byte values in a good format, and other Unix/Linux commands like xxd and hexdump weren’t printing the output the way I wanted to see it.

(They probably would have worked out eventually, but I wanted to see the byte values in the format printed by this Scala code, and it was easier to write the Scala code than it was to try to figure out how to format the output of those commands.)

I wrote these little programs because I thought there may have been some unusual characters in files that I converted from PDF to text, but in the end there were not.