方式一
package com.history.qrdemo.util
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.nio.channels.FileChannel
class QRFileUtils {
companion object {
/**
* 根据文件路径拷贝文件
* @param src 源文件
* @param destPath 目标文件路径
* @return boolean 成功true、失败false
*/
fun copyFile(src: File, desFile: File): Boolean {
if (desFile.exists()) {
val isSuccess: Boolean = desFile.delete()
if (!isSuccess) {
return false
}
}
try {
val isSuccess: Boolean = desFile.createNewFile()
if (!isSuccess) {
return false
}
} catch (e: IOException) {
e.printStackTrace()
return false
}
var result = false
var srcChannel: FileChannel? = null
var dstChannel: FileChannel? = null
try {
srcChannel = FileInputStream(src).channel
dstChannel = FileOutputStream(desFile).channel
srcChannel.transferTo(0, srcChannel.size(), dstChannel)
result = true
} catch (e: IOException) {
e.printStackTrace()
} finally {
try {
srcChannel?.close()
dstChannel?.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
return result
}
}
}