这是从文件夹中仅删除.jpg文件的任何方法吗?这是我的删除方法:
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}
Run Code Online (Sandbox Code Playgroud)
如何从文件夹中仅删除.jpg文件?
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();
}
}
Run Code Online (Sandbox Code Playgroud)
或者您更喜欢for-each版本
if (dir.isDirectory()) {
String[] children = dir.list();
for (String child : children) {
if (child.endsWith(".jpeg") || child.endsWith(".jpeg"))
new File(dir, child).delete();
}
}
Run Code Online (Sandbox Code Playgroud)