当一个功能被复用,每次这个功能需要的参数都是根据上一次该功能的元素得出的。
练习:删除一个带内容的文件夹。
思路:
1,删除一个带内容的目录,必须按照window的规则,从里往外删。
2,要删除最里面,如果做到的呢?可以使用递归。
public static void main(String[] args) {
File dir = new File("e:\\demodir");
removeDir(dir);
}
public static void removeDir(File dir){
File[] files = dir.listFiles();
for(File file : files){
if(file.isDirectory()){
removeDir(file);
}else{
System.out.println(file+":"+file.delete());
}
}
System.out.println(dir+":"+dir.delete());
}
}
五、properties类(重要)
Properties:
1, Map接口中Hashtable的子类。
2, 该类上没有泛型定义,因为它的键值都是固定的字符串类型。
3, 因为存储都是字符串数据,通常都作为属性信息存在。
4, 该集合最大的特点就是可以和IO技术相结合。也就是,该集合中的数据可以来自于流。也可以将集合中的数据写入到流中。
对Properties集合最基本演示。
public static void methodDemo_1(){
//1,创建Properties集合对象。
Properties prop = new Properties();
//2,存储元素。
prop.setProperty("wangwu", "28");
prop.setProperty("zhangsan", "20");
prop.setProperty("lisi", "34");
//取元素。所有。
Set
for(String name : nameSet){
String value = prop.getProperty(name);
System.out.println(name+":"+value);
}
}
}
//使用list方法将集合中的数据指定目的地。如指定目的地是显示器System.out
prop.list(System.out);//一般用于调试。
下面的方法很重要 开发时常用到
public static void methodDemo_3() throws IOException {
Properties prop = new Properties(); 集合
FileInputStream fis = new FileInputStream("info.txt");输入流
//将流中的数据存储到集合中。 使用集合的load方法。
prop.load(fis);
//想要对数据做修改。
prop.setProperty("wangcai", "37");其实是对文件中的键值对进行覆盖。
//想要将修改的数据保存到文件中。
// 使用store方法。
FileOutputStream fos = new FileOutputStream("info.txt");新创该文件对其覆盖。
prop.store(fos, "haha");haha部分是描述部分
fos.close();
fis.close();
prop.list(System.out);
}
六、对properties类的操作练习
练习:定义一个功能用于记录住软件运行的次数,如果运行次数大于5次。不要在运行
并给出提示:试用次数已到,请注册!给钱!
思路:
1,计数器。而且这个计数器必须软件运行结束后,持久化存储。
2,每次软件启动都要读取这个记录了计数器的数据的文件。并将计数器的值取出自增后,在重新存储。
3,数值需要名称标记,就有了键值对。这就是map集合,还要操作硬盘设备上的数据,就使用到了IO流。
map和io结合的对象正好有Properties.
代码如下:
public static void main(String[] args) throws IOException {
if(countDemo()){
//运行程序代码。
System.out.println("运行程序");
}else{
System.out.println("试用次数已到,请注册!给钱!");
}
}
public static boolean countDemo() throws IOException{
Properties prop = new Properties();集合
int count = 0;
//配置文件。
File confile = new File("config.txt");
if(!confile.exists())
confile.createNewFile();确认配置文件的存在以便读入流
FileInputStream fis = new FileInputStream(confile);
//将流中的数据加载到prop中。
prop.load(fis);
//获取配置文件中的次数。
String value = prop.getProperty("count");
if(value!=null){
count = Integer.parseInt(value);
if(count>=5){
// System.out.println("试用次数已到,请注册!给钱!");
return false;
}
}
count++;
System.out.println("运行"+count+"次");
//将具体的键和次数存储到集合中。
prop.setProperty("count", String.valueOf(count));//count+"";
//将集合中的数据写入到文件中持久化。
FileOutputStream fos = new FileOutputStream(confile);
prop.store(fos, "");
fos.close();
fis.close();
return true;
}
}
七、列出文件清单练习
思考题:
对指定目录中的所有(包含子目录)的Java文件的绝对路径写入到一个文本文件中。
这样查找某个java文件会比较便捷。建立指定文件的清单。
思路:
1,递归。
2,在递归过程需要过滤。
3,满足条件的文件很多,需要容器存储。
4,将存储的文件绝对路径写入到一个文件中。
定义功能对指定的目录进行递归。
在递归过程中需要过滤。
而且对过滤条件满足需要进行存储。
public static void listAllJavaFile(File dir, FilenameFilter filter,List
File[] files = dir.listFiles();
if(files!=null){
for(File file : files){
if(file.isDirectory()){//如果是目录就递归。
listAllJavaFile(file, filter,