可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This is any way to delete only .jpg files from folder? This is my remove method:
if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { new File(dir, children[i]).delete(); } }
How can I remove only .jpg files from folder?
回答1:
if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { String filename = children[i]; if (filename.endsWith(".jpeg") || filename.endsWith(".jpg")) new File(dir, filename).delete(); } }
or you prefer the for-each version
if (dir.isDirectory()) { String[] children = dir.list(); for (String child : children) { if (child.endsWith(".jpeg") || child.endsWith(".jpeg")) new File(dir, child).delete(); } }
回答2:
Try like this
if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { if(children[i].endsWith('.jpg' || children[i].endsWith('.jpeg')) { new File(dir, children[i]).delete(); } } }
回答3:
File dir = new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");
Then call
walkdir(dir); public void walkdir(File dir) { String Patternjpg = ".jpg"; File listFile[] = dir.listFiles(); if (listFile != null) { for (int i = 0; i < listFile.length; i++) { if (listFile[i].isDirectory()) { walkdir(listFile[i]); } else { if (listFile[i].getName().endsWith(Patternjpg)){ //Do what ever u want listFile[i].delete(); } } } } }