java用部分匹配删除文件-根据日期删除文件

转载来源:http://blog.csdn.net/bestcxx/article/details/50298637
public class A {

	// 指定文件保存的位置
	private final String savePath = "c:/a/";
	// 查询并删除所有今天生成的文件
	public int deleteFile() {
		int number = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String date = sdf.format(new Date());
		File file = new File(savePath);
		// list():指定此抽象路径名表示的目录中的文件和目录
		// 除非是空目录,才能删除,否则是假删除,实际没有被删除
		String[] templist = file.list();
		System.out.println(templist);
		File temp = null;
		for (int i = 0; i < templist.length; i++) {
			// path完整路径: c:/a/20180313abc.txt
			String path = savePath + templist[i];
			temp = new File(path);
			// startsWith:用来检测某字符串是否以另一个字符串开始
			if (temp.getName().startsWith(date)) {
				System.out.println(temp.getName());
				temp.delete();
				number++;
			}
		}
		return number;
	}

	public static void main(String[] args) {
		A a = new A();
		int n = a.deleteFile();
		System.out.println("删除文件的个数为:" + n);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值