当前位置:网站首页>Recursively traverse directory structure and tree presentation

Recursively traverse directory structure and tree presentation

2022-06-26 05:15:00 Light ink CGZ

code snippet , Note the code example address . Is custom .

import java.io.File;
public class TestFile6 {
    
    public static void main(String[] args) {
    
        File f = new File("d:/ The movie ");
        printFile(f, 0);
    }
    /** *  Print file information  * @param file  File name  * @param level  Number of layers ( The actual is : The number of recursive calls ) */
    static void printFile(File file, int level) {
    
        // Number of output layers 
        for (int i = 0; i < level; i++) {
    
            System.out.print("-");
        }
        // Output file name 
        System.out.println(file.getName());
        // If file Is a directory , Then get the sub file list , And do the same operation for each sub file 
        if (file.isDirectory()) {
    
            File[] files = file.listFiles();
            for (File temp : files) {
    
                // Call the method recursively : Pay attention to wait +1
                printFile(temp, level + 1);
            }
        }
    }
}

 Insert picture description here  Insert picture description here

原网站

版权声明
本文为[Light ink CGZ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260505081026.html