Created
April 20, 2023 10:03
-
-
Save aartajew/87bbb0a508d20f5ea2468eb361763c52 to your computer and use it in GitHub Desktop.
Scala > Encode file to base64
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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