Java - 递归 - 和文件流关联的集合 - Properties

递归小练习:

public class RemoveDirTest {
	/***
	 * 删除指定目录下的所有文件 对文件的操作要用到 File类 删除所有要深度遍历删除,从里面往外面删
	 ****/
	public static void main(String[] args) {

		File dir = new File("abcd");
		removeDir(dir);
	}
	private static void removeDir(File dir) {

		if (!(dir.isDirectory())) {
			dir.delete();
			return;
		}
		File[] files = dir.listFiles();
		for (File file : files) {
			if (file.isDirectory())
				removeDir(file);
			else
				file.delete();
		}
		dir.delete();
	}
}

Properties集合的特点:

   1.数据持久化

   2.和流关联

   3.操作字符串

方法:

   1.list()-----直接把集合的数据打印到控制台。

   2.store()---将集合的数据写入输出流,保存。

   3.list()------从输入流中读取数据到集合中。

public class ProPertiesTest {
	/*
	 * 记录程序运行的次数,超过5次就提醒用户注册。
	 * 
	 * 记录运行次数要有一个计数器count。并且count应该持久保存。使用ProPerties。。。
	 * 
	 * 第一次运行程序的时候,创建一个count.properties配置文件。写入count信息。 以后每次运行读取配置文件并且,count++
	 * 如果count次数超过5,就提醒。。。
	 */
	public static void main(String[] args) throws IOException {

		// 创建一个count的配置文件
		File conf = new File("count.properties");
		// 如果文件不存在就自己创建一个
		if (!(conf.exists()))
			conf.createNewFile();
		//定义一个计数器
		int count = 0;
		
		/*每次运行的时候先读取配置文件到集合中,获取count值。*/
		
		//创建一个集合
		Properties pro = new Properties();
		//创建一个读取流
		FileReader fr = new FileReader(conf);
		//加载到集合中
		pro.load(fr);
		//获取集合的保存在文件中的count值
		String value = pro.getProperty("count");		
		
		//第一次运行后
		if(value!=null){
			
			count = Integer.parseInt(value);
			//如果count大于5,就提醒
			if(count>=5)
				throw new RuntimeException("请先注册");			
		}	
		
		//第一次运行时
		count++;
		//将计数器保存到集合中
		pro.setProperty("count", count+"");
		//创建输出流
		FileWriter fw = new FileWriter(conf);
		//把集合数据写到文件中
		pro.store(fw, "");
	}

}

综合小练习

/*
 * 获取指定目录下所有指定文件的绝对路径。并将信息写入到一个文本文件中。
 * 
 * 深度遍历指定目录,过滤文件,获取所有指定文件
 * 
 * 保存信息
 * 
 */
public class Test {

	public static void main(String[] args) throws IOException {

		File f1 = new File("abcd");
		// 创建一个容器用于存放指定文件
		List<File> list = new ArrayList<File>();
		// 创建一个过滤器
		FilenameFilter filter = new FilterByName(".txt");
		// 获取指定目录下的指定文件
		getFileByName(f1, list, filter);
		// 输出到指定文件
		write2file(list);
	}

	private static void write2file(List<File> list) throws IOException {

		BufferedWriter bufw = null;
		try {
			bufw = new BufferedWriter(new FileWriter("path.txt"));
			for (File file : list) {
				bufw.write(file.getAbsolutePath());
				bufw.newLine();
				bufw.flush();
			}
		} finally {
			bufw.close();
		}
	}

	private static void getFileByName(File f1, List<File> list,
			FilenameFilter filter) {

		File[] files = f1.listFiles();
		for (File file : files) {
			if (file.isDirectory()) {
				getFileByName(file, list, filter);
			} else {
				if (filter.accept(f1, file.getName()))
					list.add(file);
			}
		}
	}
}












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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值