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()); 
        } 
    } 

You May Also Like