Skip to content

Instantly share code, notes, and snippets.

@aartajew
Created April 20, 2023 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aartajew/87bbb0a508d20f5ea2468eb361763c52 to your computer and use it in GitHub Desktop.
Save aartajew/87bbb0a508d20f5ea2468eb361763c52 to your computer and use it in GitHub Desktop.
Scala > Encode file to base64
package util
import java.io._
import java.util._
object Base64File {
def encode(sourcePath: String, targetPath: String) = {
val inputStream = new FileInputStream(sourcePath)
val outputStream = new FileOutputStream(targetPath)
val buffer = new Array[Byte](3000) // must be multiple of 3 bytes
while (inputStream.read(buffer) >= 0) {
val encoded = Base64.getEncoder.encode(buffer)
outputStream.write(encoded)
}
inputStream.close()
outputStream.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment