|
|
@ -13,28 +13,20 @@ import java.io.IOException |
|
|
|
|
|
|
|
|
|
|
|
object FileUtils { |
|
|
|
object FileUtils { |
|
|
|
|
|
|
|
|
|
|
|
fun exists(file: File, fileName: String, vararg subDirs: String): Boolean { |
|
|
|
fun exists(root: File, fileName: String, vararg subDirs: String): Boolean { |
|
|
|
val filePath = |
|
|
|
return getFile(root, fileName, subDirs = *subDirs).exists() |
|
|
|
file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName |
|
|
|
|
|
|
|
return File(filePath).exists() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun createFileIfNotExist(file: File, fileName: String, vararg subDirs: String): File { |
|
|
|
fun createFileIfNotExist(root: File, fileName: String, vararg subDirs: String): File { |
|
|
|
val filePath = |
|
|
|
val filePath = getPath(root, fileName, *subDirs) |
|
|
|
file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName |
|
|
|
|
|
|
|
return createFileIfNotExist(filePath) |
|
|
|
return createFileIfNotExist(filePath) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun createFileIfNotExist(file: File, vararg subDirs: String): File { |
|
|
|
fun createFileIfNotExist(root: File, vararg subDirs: String): File { |
|
|
|
val filePath = file.absolutePath + File.separator + subDirs.joinToString(File.separator) |
|
|
|
val filePath = root.absolutePath + File.separator + subDirs.joinToString(File.separator) |
|
|
|
return createFolderIfNotExist(filePath) |
|
|
|
return createFolderIfNotExist(filePath) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getCachePath(): String { |
|
|
|
|
|
|
|
return App.INSTANCE.externalCacheDir?.absolutePath |
|
|
|
|
|
|
|
?: App.INSTANCE.cacheDir.absolutePath |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun createFolderIfNotExist(filePath: String): File { |
|
|
|
fun createFolderIfNotExist(filePath: String): File { |
|
|
|
val file = File(filePath) |
|
|
|
val file = File(filePath) |
|
|
|
//如果文件夹不存在,就创建它 |
|
|
|
//如果文件夹不存在,就创建它 |
|
|
@ -62,6 +54,24 @@ object FileUtils { |
|
|
|
return file |
|
|
|
return file |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getFile(file: File, fileName: String, vararg subDirs: String): File { |
|
|
|
|
|
|
|
val filePath = getPath(file, fileName, *subDirs) |
|
|
|
|
|
|
|
return File(filePath) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getFile(root: File, vararg subDirs: String): File { |
|
|
|
|
|
|
|
val filePath = getPath(root, subDirs = *subDirs) |
|
|
|
|
|
|
|
return File(filePath) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getPath(root: File, fileName: String? = null, vararg subDirs: String): String { |
|
|
|
|
|
|
|
return if (fileName.isNullOrEmpty()) { |
|
|
|
|
|
|
|
root.absolutePath + File.separator + subDirs.joinToString(File.separator) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
root.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//递归删除文件夹下的数据 |
|
|
|
//递归删除文件夹下的数据 |
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
fun deleteFile(filePath: String) { |
|
|
|
fun deleteFile(filePath: String) { |
|
|
@ -79,6 +89,11 @@ object FileUtils { |
|
|
|
file.delete() |
|
|
|
file.delete() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getCachePath(): String { |
|
|
|
|
|
|
|
return App.INSTANCE.externalCacheDir?.absolutePath |
|
|
|
|
|
|
|
?: App.INSTANCE.cacheDir.absolutePath |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getSdCardPath(): String { |
|
|
|
fun getSdCardPath(): String { |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
var sdCardDirectory = Environment.getExternalStorageDirectory().absolutePath |
|
|
|
var sdCardDirectory = Environment.getExternalStorageDirectory().absolutePath |
|
|
|