【发布时间】:2020-03-29 15:32:18
【问题描述】:
嗨,我有一个很大的字节数组,我想转换为 sdcard 中的文件我使用了这段代码,但有时会崩溃 将字节数组转换为 kotlin 中的文件的最佳方法是什么?
private fun writeBytesToFileClassic(
bFile: ByteArray,
fileDest: String
) {
var fileOuputStream: FileOutputStream? = null
try {
fileOuputStream = FileOutputStream(fileDest)
fileOuputStream.write(bFile)
} catch (e: IOException) {
e.printStackTrace()
} finally {
if (fileOuputStream != null) {
try {
fileOuputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
fun ExtractPdf(byteArray: ByteArray, filename: String){
var fileInputStream: FileInputStream? = null
val file: File = File(UPLOAD_FOLDER())
if (!file.exists()){
file.mkdir()
}
try {
//save bytes[] into a file
writeBytesToFileClassic(byteArray, UPLOAD_FOLDER() + filename)
} catch (e: IOException) {
e.printStackTrace()
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
【问题讨论】:
标签: java arrays kotlin fileinputstream fileoutputstream