清除目錄下檔案

1 篇文章 / 0 new
author
清除目錄下檔案
     void cleanDirectory() {
        File tarDir = this.getCacheDir();//起始目錄
        if(!tarDir.exists()) {
            return;
        }
 
         String fList[] = tarDir.list();
         if(fList == null) {
             tarDir.delete();//刪除目錄
             return;
         }
         for(int i = 0; i < fList.length; i++) {
             File file = new File(tarDir, fList[i]);
             if(file.isDirectory()) {
                 cleanSubDirectory(tarDir, fList[i]);
             } else {
                 file.delete();
             }
         }
     }
 
     void cleanSubDirectory(File sDir, String dirName) {
       File tarDir = new File(sDir,  dirName);
       if(!tarDir.exists()) {
           return;
       }
        String fList[] = tarDir.list();
        for(int i = 0; i < fList.length; i++) {
            File file = new File(tarDir, fList[i]);
            if(file.isDirectory()) {
                cleanSubDirectory(tarDir, fList[i]);
            } else {
                file.delete();
            }
        }
        tarDir.delete();
    }
Free Web Hosting