2011-03-08 131 views
3

我有下面的代碼,它檢查是否存在於SD卡上,如果文件夾存在,我想添加另一個if語句來檢查實際文件夾中是否有zip文件實際上存在。我能做些什麼來檢查文件夾中的zip擴展名。該文件夾應該有很多的拉鍊,但我只希望它檢查以確保有拉鍊和沒有其他文件擴展名。我感謝您對此提供任何幫助。Java壓縮文件的檢查目錄

File z = new File("/mnt/sdcard/folder"); 
if(!z.exists()) { 
Toast.makeText(MainMethod.this, "/sdcard/folder Not Found!", Toast.LENGTH_LONG).show(); 
} else { 
Toast.makeText(MainMethod.this, "/sdcard/folder Found!", Toast.LENGTH_LONG).show(); 
} 

編輯: 謝謝你們在這裏幫助是我結束了在你的幫助使用,我沒有測試它尚未但它看起來不錯。

File z = new File("/mnt/sdcard/Folder"); 
    if(!z.exists()) { 
      //create folder 
} else { 
     FilenameFilter f2 = new FilenameFilter() { 
     public boolean accept(File dir, String filename) { 
     return filename.endsWith("zip"); 
     } 
     }; 
      if (z.list(f2).length > 0) { 
      // there's a zip file in there.. 
      } else { 
      //no zips inside folder 
     } 
    } 

回答

6
File f = new File("folder"); 
FilenameFilter f2 = new FilenameFilter() { 
public boolean accept(File dir, String filename) { 
return filename.endsWith("zip"); 
} 
}; 
if (f.list(f2).length > 0) { 
// there's a zip file in there.. 
} 

試試上面..

4

你看過FileNameFilter

File f = new File("/mnt/sdcard/folder"); 
if(e.exist()){//file exist ?? 

File[] matchingFiles = f.listFiles(new FilenameFilter() { 
    public boolean accept(File dir, String name) { 
     return name.endsWith("zip"); 
    } 
});//list out files with zip at the end 

}