代码之家  ›  专栏  ›  技术社区  ›  Farhan Islam

如何在Scala中以5MB块的迭代从本地文件写入二进制文件?

  •  0
  • Farhan Islam  · 技术社区  · 6 年前

    我目前正在尝试获取一个15MB的视频文件,该文件存储在我的计算机本地,并将该文件以二进制文件格式写入到5MB的块中。我正试图让它适用于任何文件大小,它将较大的文件格式拆分成较小的5MB块。

        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()
    
    0 回复  |  直到 6 年前