riku
2022-06-17 3a5c011d9509d3bc0367921f463676c81ff2e374
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package cn.flightfeather.supervision.common.utils
 
import java.io.File
import java.io.FileOutputStream
import java.io.FileInputStream
 
 
 
 
class 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)
                }
            }
 
        }
    }
}