Farhan Islam
Farhan Islam

Reputation: 649

How can you write to a binary file from a local file in iterations of 5MB Chunks in Scala?

I am currently trying to take a 15MB video file that is stored locally on my machine and write that file into chunks of 5MB in binary file format. I am trying to get this to work for any file size to where it splits up larger file formats into smaller 5MB Chunks.

    val chunksize: Int = 5242880
    val byteArray = Files.readAllBytes(Paths.get("/path/to/inputfile"))
    val fos = new FileOutputStream("/path/to/outputfile")
    val ret = new Array[Array[Byte]](Math.ceil(byteArray.length / chunksize.asInstanceOf[Double]).toInt, chunksize)
    var start = 0
    var i = 0
    while ( {
      i < ret.length
    }) {
      ret(i) = util.Arrays.copyOfRange(byteArray, start, start + chunksize)
      start += chunksize

      //Here is where i need to write the bytes to a file output stream so I can get a binary file of less than 5MB

      {
        i += 1; i - 1
      }

    }

    fos.close()

Upvotes: 0

Views: 131

Answers (0)

Related Questions