1 删除一个带内容的目录
分析:
原理:必须从最里面往外删,需要进行深度遍历 — 递归
要利用 File类的 delete()方法 删除目录时,必须保证该目录下没有文件或者子目录,否则删除失败
所以,要用递归,先删除该目录下的所有子目录和文件,然后再删除该目录
private static void removeDir(File f) {
// 先获取所有的目录
File[] files = f.listFiles();
for(File file:files){
if(file.isDirectory()){
removeDir(file); // 遍历,删除所有子目录
} else
System.out.println(file+":"+file.delete()); // 如果是文件,就删除文件,
}
System.out.println(f+":"+f.delete()); // 把除了文件的文件夹删掉
}
运行结果:
E:\testFile\a.txt:true
E:\testFile\a\c.txt:true
E:\testFile\a\b\s\s.txt:true
E:\testFile\a\b\s:true
E:\testFile\a\b:true
E:\testFile\a\c:true
E:\testFile\a:true
E:\testFile:true
2 深度遍历文件夹
分析:
对指定目录进行所有内容的列出(包括子目录及其内容),递归,分两步
1 先获取当前目录的,所有文件夹或者文件对象
2 在列目录的时候,判断一下,如果有二级目录,接着遍历
private static void listAll(File f) {
// 获取指定目录下 当前的所有文件夹或者文件对象,每传一个,就打印一个
System.out.println( f.getAbsolutePath());
File[] files = f.listFiles();
for(int x=0;x<files.length;x++){
// 在列目录的时候,先判断一下,如果有二级目录,接着遍历 --- 递归
if(files[x].isDirectory()){
listAll(files[x]); // 递归
} else
System.out.println(files[x].getAbsolutePath());
}
}
运行结果:
F:\360
F:\360\360Safe
F:\360\360Safe\ipc
F:\360\360Safe\ipc\filecache
F:\360\360Safe\ipc\filecache\FileCache.dat
F:\360\360Safe\ipc\filecache\FileCache.dat.LOG1
F:\360\360Safe\ipc\filecache\FileCache.dat.LOG2
F:\360\360Safe\ipc\filecache\FileCache.dat{7abfaa8d-1ac7-11e8-9deb-c8d3ff76e3e5}.TM.blf
F:\360\360Safe\ipc\filecache\FileCache.dat{7abfaa8d-1ac7-11e8-9deb-c8d3ff76e3e5}.TMContainer00000000000000000001.regtrans-ms
F:\360\360Safe\ipc\filecache\FileCache.dat{7abfaa8d-1ac7-11e8-9deb-c8d3ff76e3e5}.TMContainer00000000000000000002.regtrans-ms
F:\360\360Safe\safemon
F:\360\360Safe\safemon\safemon64.dll
F:\360\360Safe\safemon\safewrapper.dll
F:\360\360softmgr.exe
F:\360\setupbeta_11.5.0.1004n.exe
如果不想用 getAbsolutePath(),用 getName()也可以,但是打印出来,没有缩进,怎么做到?写个计数器,每多一级,就缩进距离
listAll(f,0);
private static void listAll(File f, int i) {
// 获取指定目录下,当前的所有文件夹或者文件对象
System.out.println(getSpace(i)+f.getName());
i++;
File[] files = f.listFiles();
for(int x=0;x<files.length;x++){
if(files[x].isDirectory()){
listAll(files[x],i);
} else{
System.out.println(getSpace(i)+files[x].getName());
}
}
}
// 增加缩进 空格的方法
private static String getSpace(int i) {
StringBuilder sb= new StringBuilder();
sb.append("|--");
for(int x=0;x<i;x++){
sb.insert(0,"| ");
}
return sb.toString();
}
运行结果:
|--360
| |--360Safe
| | |--ipc
| | | |--filecache
| | | | |--FileCache.dat
| | | | |--FileCache.dat.LOG1
| | | | |--FileCache.dat.LOG2
| | | | |--FileCache.dat{7abfaa8d-1ac7-11e8-9deb-c8d3ff76e3e5}.TM.blf
| | | | |--FileCache.dat{7abfaa8d-1ac7-11e8-9deb-c8d3ff76e3e5}.TMContainer00000000000000000001.regtrans-ms
| | | | |--FileCache.dat{7abfaa8d-1ac7-11e8-9deb-c8d3ff76e3e5}.TMContainer00000000000000000002.regtrans-ms
| | |--safemon
| | | |--safemon64.dll
| | | |--safewrapper.dll
| |--360softmgr.exe
| |--setupbeta_11.5.0.1004n.exe
3 Properties 练习
定义一个功能,获取一个应用程序运行的次数,如果超过 5次,给出 使用次数已到,请注册 的提示,并不再运行程序
思路:
1 应该有计数器每次程序启动,都要计数一次,并且是在原有的次数上进行计数
2 计数器的思考
计数器是一个变量,如果计数器存在内存当中,程序启动的时候进行计数,可是程序一结束,计数器消失了
再次启动该程序,计数器又被重新初始化
我们需要的是,多次启动同一个应用程序,使用的是 同一个计数器
这就需要计数器生命周期变长,从内存 存储到 硬盘文件中3 如何使用计数器
首先,程序启动时,应该先读取这个用于记录计数器信息的配置文件,获取上一次计数器次数,并进行试用次数的判断
其次,对该次数进行自增,并将自增后的次数,重新存储到配置文件中4 文件中的信息 该如何进行存储 并体现
直接存储次数值可以,但是不明确该数据的含义,所以,起名变得很重要
这时,可以使用键值对 — 名和值相对应
映射关系用 Map,又需要读取硬盘上的数据,所以 考虑 Properties
Properties = Map + io
private static void getAppCount() throws IOException {
// 将配置文件 封装成 File对象
File confile = new File("E:\\count.properties");
if(!confile.exists()){
confile.createNewFile();
}
FileInputStream fis = new FileInputStream(confile);
Properties prop = new Properties();
prop.load(fis);
// 从集合中 通过键 获取次数
String value = prop.getProperty("time");
// 定义计数器 记录获取到的次数
int count = 0;
if(value !=null){
count = Integer.parseInt(value);
if(count>5){
System.out.println("使用次数已到,请注册!");
}
}
count++;
// 将改变后的次数,重新存储到集合中
prop.setProperty("time",count+"");
FileOutputStream fos = new FileOutputStream(confile);
prop.store(fos, "");
fos.close();
fis.close();
}