name=zx1001.txt
absoluteFile=/storage/emulated/0/Android/data/zx.com.demo/files/zx1001.txt
absolutePath=/storage/emulated/0/Android/data/zx.com.demo/files/zx1001.txt
path=/storage/emulated/0/Android/data/zx.com.demo/files/zx1001.txt
parent=/storage/emulated/0/Android/data/zx.com.demo/files
重命名是否成功false,name=zx1001.txt
文件检测
val file = File(getExternalFilesDir(“”)?.absolutePath + “/app-debug.apk”)
val dir = File(getExternalFilesDir(“”)?.absolutePath)
println(“exists=” + file.exists())
println(“canExecute=” + file.canExecute())
println(“canRead=” + file.canRead())
println(“canWrite=” + file.canWrite())
println(“isDirectory=” + file.isDirectory)
println(“lastModified=” + Date(file.lastModified()))
println(“length=” + file.length())
println(“---------”)
println(“dir.isDirectory=” + dir.isDirectory)
println(“dir.lastModified=” + Date(dir.lastModified()))
println(“dir.length=” + dir.length())
输出
exists=true
canExecute=false
canRead=true
canWrite=true
isDirectory=false
lastModified=Fri Jun 11 14:05:38 GMT+08:00 2021
length=7785185
dir.isDirectory=true
dir.lastModified=Fri Jun 11 14:13:14 GMT+08:00 2021
dir.length=4096
注意,.length()
只能返回文件的大小(单位为字节),文件夹的实际大小无法返回。
文件夹操作
val dir = File(getExternalFilesDir(“”)?.absolutePath + “/myDir/dir1”)
dir.mkdirs()//创建一个文件目录。若上层文件目录不存在,一并创建
val appDir = File(getExternalFilesDir(“”)?.absolutePath)
dir.mkdirs()
val files = appDir.listFiles() //返回目录下的文件列表
for (i in files) {
println(i)
}
val filePaths = appDir.list()//返回目录下文件名和文件夹名称数组
for (i in filePaths) {
println(i)
}
输出
/storage/emulated/0/Android/data/zx.com.demo/files/myDir
/storage/emulated/0/Android/data/zx.com.demo/files/zx1001.txt
/storage/emulated/0/Android/data/zx.com.demo/files/app-