在 kotlin 中将大字节数组转换为文件
Convert large bytesarray to file in kotlin
嗨,我有一个大字节数组,我想转换为 SD 卡中的文件我使用了这段代码,但有时会崩溃将字节数组转换为 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()
}
}
}
}
你可以使用 apache commons-io 库
FileUtils.writeByteArrayToFile(File, ByteArray)
您可以使用writeBytes函数:
fun File.writeBytes(array: ByteArray)
嗨,我有一个大字节数组,我想转换为 SD 卡中的文件我使用了这段代码,但有时会崩溃将字节数组转换为 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()
}
}
}
}
你可以使用 apache commons-io 库
FileUtils.writeByteArrayToFile(File, ByteArray)
您可以使用writeBytes函数:
fun File.writeBytes(array: ByteArray)