当前位置:操作系统 > 安卓/Android >>

Android遍历特定目录下所有文件,包含子目录的, 并删除最新创建的

直接上代码了..
[java]
private boolean deleteLastFromFloder(String path) { 
    boolean success = false; 
    try { 
        ArrayList<File> images = new ArrayList<File>(); 
        getFiles(images, path); 
        File latestSavedImage = images.get(0); 
        if (latestSavedImage.exists()) { 
            for (int i = 1; i < images.size(); i++) { 
                File nextFile = images.get(i); 
                if (nextFile.lastModified() > latestSavedImage.lastModified()) { 
                    latestSavedImage = nextFile; 
                } 
            } 
 
            Log.e("brady", "images = " + latestSavedImage.getAbsolutePath()); 
            success = latestSavedImage.delete(); 
        } 
    } catch (Exception e) { 
        e.printStackTrace(); 
    } 
    return success; 

 
private void getFiles(ArrayList<File> fileList, String path) { 
    File[] allFiles = new File(path).listFiles(); 
    for (int i = 0; i < allFiles.length; i++) { 
        File file = allFiles[i]; 
        if (file.isFile()) { 
            fileList.add(file); 
        } else if (!file.getAbsolutePath().contains(".thumnail")) { 
            getFiles(fileList, file.getAbsolutePath()); 
        } 
    } 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,