Discuss / Java / 练习

练习

Topic source

Lier

#1 Created at ... [Delete] [Delete and Lock User]
public class Main {
    public static void main(String[] args) throws IOException {
       File file = new File("src");
       printFiles(file);
    }

    static void printFiles(File file){
       int i=0;
       printFileEnd(file,i);
    }

    static void printFileEnd(File file,int i){
       String prefix="";
       for (int j = 0; j < i; j++) {
          prefix += "\t";
       }
       i++;
       if(file.exists()){
          System.out.println(prefix+file.getName()+File.separator);
          File[] files = file.listFiles();
          if(files!=null){
             for (File file1 : files) {
                if(file1.isDirectory())
                   printFileEnd(file1,i);
                else
                   System.out.println(prefix+file1.getName());
             }
          }
       }
    }
}

  • 1

Reply