package file;
import java.io.File;
/**
* 输出某个文件夹下所有某个格式的文件
* @author hasee
*
*/
public class Demo2 {
public static void main(String[] args) {
getTxtName("d:/a",".jpg");
}
public static void getTxtName(String path,String suffix) {
//判断文件对象是文件还是文件夹
//构建文件对象
File f = new File(path);
//根据文件或者文件夹处理
if(f.isFile()) {
if(f.getName().endsWith(suffix)) {
System.out.println(f.getAbsolutePath());
}
}else {
//遍历文件夹
File[] files = f.listFiles();
if(files!=null && files.length>0) {
//继续递归得到的文件或文件夹
for (File file : files) {
getTxtName(file.getAbsolutePath(),suffix);
}
}
}
}
}
2.删除文件夹下某个格式的所有文件
package file;
import java.io.File;
public class Demo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
delete("d:/a",".jpg");
}
public static void delete(String path,String suffix) {
File f = new File(path);
if(f.isFile()) {
if(f.getName().endsWith(suffix)) {
System.out.println(f.getAbsolutePath()+"成功删除");
f.delete();
}
}else {
File[] files = f.listFiles();
if(files!=null&&files.length>0) {
for (File file : files) {
delete(file.getAbsolutePath(),suffix);
}
}
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有