************************************* 不删数据 *************************************
public static void recursion(String path){
File directory = new File(path);
for(File file : directory.listFiles()){
if(file.isFile()){
System.out.println(file.getName());
}else{
recursion(file.getPath());
}
}
}
public static void recursionToStack(String path){
List<String> list = new ArrayList<>();
list.add(path);
while(!list.isEmpty()){
File directory = new File(list.get(0));
for(File file : directory.listFiles()){
if(file.isFile()){
System.out.println(file.getName());
}else{
list.add(file.getPath());
}
}
list.remove(0);
}
}
************************************* 删除数据 *************************************
public static void recursion(String path){
File directory = new File(path);
for(File file : directory.listFiles()){
if(file.isFile()){
System.out.println(file.getName());
file.delete();
}else{
recursion(file.getPath());
}
}
directory.delete();
}
public static void recursionToStack(String path){
List<String> list = new ArrayList<>();
list.add(path);
while(!list.isEmpty()){
boolean bCanDelete = true;
File directory = new File(list.get(list.size()-1));
for(File file : directory.listFiles()){
if(file.isFile()){
System.out.println(file.getName());
file.delete();
}else{
list.add(file.getPath());
bCanDelete= false;
}
}
if(bCanDelete){
list.remove(list.size()-1);
directory.delete();
}
}
}