删除文件或文件夹(指定path)

本文介绍了一种使用Java实现的方法,能够递归地扫描并删除指定路径下的所有带有特定后缀的文件或文件夹。该方法适用于清理大量不需要的文件,如备份文件或临时文件等。
	//删除文件或文件夹
	public static void scanAllFile(File root, String... suffix) {
		for (File file : root.listFiles()) {
			if (isMatch(file.getName(), suffix)) {
				delAllFiles(file);
			} else if (file.isDirectory()) {
				scanAllFile(file, suffix);
			}
		}
	}
	// 匹配类型
	public static boolean isMatch(String name, String... suffix) {
		for (String s : suffix) {
			if (name.endsWith(s)) {
				return true;
			}
		}
		return false;
	}
	// 清空指定路径下所有的文件或文件夹
	public static void delAllFiles(File file) {
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File f : files) {
				if (f.isDirectory()) {
					delAllFiles(f);
				} else {
					f.delete();
				}
			}
		}
		file.delete();
	}
	// 找到指定目录的所有文件
	public static String findAllFiles(String path) {
		String fileName = path;
		File file = new File(path);
		File[] files = file.listFiles();
		if (files.length > 0) {
			fileName += "\\" + files[0].getName();
			System.out.println(fileName);
		}
		return fileName;
	}
	public static void main(String[] args) {
		scanAllFile(new File("F:\\est\\xml"), ".svn");
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值