java_file之查询file类查后缀名跟删除

前几天在查了一些基础的面试题.
然后就在今天比较有空的时间下写了一小段代码.作为笔记,
package r;

import java.io.File;

public class R {

	public static void main(String[] args) {
		File f = new File("c:" + File.separator + "test");
		test(f);
	}

	public static void test(File f) {

		File fs[] = f.listFiles();// 列举文件
		if (f.exists()) {
			for (int i = 0; i < fs.length; i++) {
				if (fs[i].isDirectory()) {
					test(fs[i]);

				}
				// String name = (String) fs[i].getName();
				// String p = name.substring(name.lastIndexOf("/") + 1);

				if (fs[i].getName().endsWith(".java")) {
					System.out.println(fs[i]);
				}

			}

		}

	}
}

下面则是删除整个文件夹
package com.demo.test1;

import java.io.File;
import java.io.IOException;

public class R {

	public static void main(String[] args) {
		File f = new File("c:" + File.separator + "test");

		test(f);

	}

	static int count = 0;
	static int fcount = 0;

	public static void test(File f) {
		File[] fs = f.listFiles();

		if (f.exists()) {
			for (int i = 0; i < fs.length; i++) {
				if (fs[i] != null) {
					if (fs[i].isDirectory()) {
						count += 1;
						test(fs[i]);
						fs[i].delete();
					} else {

						fcount += 1;
					}
				}

				fs[i].delete();

			}
			f.delete();
			// f1.delete();
			System.out.println("文件夹的数量为: " + count);
			System.out.println("文件的数量为: " + fcount);
		} else {

			System.out.println("不存在该文件夹");
		}

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值