package cn.flightfeather.supervision.common.utils
|
|
import java.io.File
|
import java.io.FileOutputStream
|
import java.io.FileInputStream
|
|
|
|
|
object FileUtil {
|
@Throws(Exception::class)
|
fun uploadFile(file: ByteArray, filePath: String, fileName: String) {
|
val targetFile = File(filePath)
|
if (!targetFile.exists()) {
|
targetFile.mkdirs()
|
}
|
val out = FileOutputStream(filePath + fileName)
|
out.write(file)
|
out.flush()
|
out.close()
|
}
|
|
//删除文件夹
|
fun delFolder(folderPath: String) {
|
try {
|
delAllFile(folderPath) //删除完里面所有内容
|
var filePath = folderPath
|
filePath = filePath
|
val myFilePath = java.io.File(filePath)
|
myFilePath.delete() //删除空文件夹
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
|
}
|
|
fun delAllFile(path: String): Boolean {
|
var flag = false
|
val file = File(path)
|
if (!file.exists()) {
|
return flag
|
}
|
if (!file.isDirectory) {
|
return flag
|
}
|
val tempList = file.list()
|
var temp: File? = null
|
for (i in tempList!!.indices) {
|
if (path.endsWith(File.separator)) {
|
temp = File(path + tempList[i])
|
} else {
|
temp = File(path + File.separator + tempList[i])
|
}
|
if (temp.isFile) {
|
temp.delete()
|
}
|
if (temp.isDirectory) {
|
delAllFile(path + "/" + tempList[i])//先删除文件夹里面的文件
|
delFolder(path + "/" + tempList[i])//再删除空文件夹
|
flag = true
|
}
|
}
|
return flag
|
}
|
|
@Throws(Exception::class)
|
fun copy(file: File, toFile: File) {
|
val b = ByteArray(1024)
|
var a: Int
|
val fis: FileInputStream
|
val fos: FileOutputStream
|
if (file.isDirectory) {
|
var filepath = file.absolutePath
|
filepath = filepath.replace("\\\\".toRegex(), "/")
|
var toFilepath = toFile.absolutePath
|
toFilepath = toFilepath.replace("\\\\".toRegex(), "/")
|
val lastIndexOf = filepath.lastIndexOf("/")
|
toFilepath += filepath.substring(lastIndexOf, filepath.length)
|
val copy = File(toFilepath)
|
//复制文件夹
|
if (!copy.exists()) {
|
copy.mkdir()
|
}
|
//遍历文件夹
|
for (f in file.listFiles()!!) {
|
copy(f, copy)
|
}
|
} else {
|
if (toFile.isDirectory) {
|
var filepath = file.absolutePath
|
filepath = filepath.replace("\\\\".toRegex(), "/")
|
var toFilepath = toFile.absolutePath
|
toFilepath = toFilepath.replace("\\\\".toRegex(), "/")
|
val lastIndexOf = filepath.lastIndexOf("/")
|
toFilepath += filepath.substring(lastIndexOf, filepath.length)
|
|
//写文件
|
val newFile = File(toFilepath)
|
fis = FileInputStream(file)
|
fos = FileOutputStream(newFile)
|
a = fis.read(b)
|
while (a != -1) {
|
fos.write(b, 0, a)
|
a = fis.read(b)
|
}
|
} else {
|
//写文件
|
fis = FileInputStream(file)
|
fos = FileOutputStream(toFile)
|
a = fis.read(b)
|
while (a != -1) {
|
fos.write(b, 0, a)
|
a = fis.read(b)
|
}
|
}
|
|
}
|
}
|
}
|