递归 查找 一个文件夹所有的子文件夹和文件
程序员文章站
2022-07-05 08:38:42
...
- import java.io.*;
- public class RecursionTest{
- public static void main(String[] args){
- String fullFileName = "c:/A";
- File f = new File(fullFileName);
- p(f.getName());
- int count = 1;
- readFile(f, count);
- }
- private static void readFile(File f, int count){
- String pre = "";
- for(int i=0; i<count; i++){
- pre+=" ";
- }
- if(f.exists()){
- File[] fs = f.listFiles();
- for(int i=0; i<fs.length; i++){
- p(pre + fs[i].getName());
- if(fs[i].isDirectory()){
- readFile(fs[i], count + 1);
- }else if(fs[i].isFile()){
- }
- }
- }else{
- p("不存在");
- }
- }
- private static void p(Object o){
- System.out.println(o.toString());
- }
- }