Android之最简单的遍历某个目录下的所有文件(递归)

1、问题

遍历某个目录下的所有问题文件

2、代码实现

    fun getRecoverTrashFile(path: String) {
        if (TextUtils.isEmpty(path))
            return
        try {
            var file = File(path)
            if (file == null || !file.exists()) {
                return
            }
            var files = file.listFiles()
            if (files == null || files.size <= 0) {
                return
            }
            for (i in files.indices) {
                if (files[i].isDirectory) {
                    var path = files[i].absolutePath
                    getRecoverTrashFile(path)
                } else {
                    var file = files[i]
                    addTrashFileByPath(file)
                }
            }
        } catch (e: Exception) {
            LogUtil.i(TAG, "has error")
            e.printStackTrace()
        }
    }

 

猜你喜欢

转载自blog.csdn.net/u011068702/article/details/108762824