列出目录下所有文件和目录

列出目录下所有文件和目录


import java.io.File;

public class Test {
    public static void main(String[] args) throws Exception {
        showDirectory(new File("/Users/apple/redis"));
    }

    private static void showDirectory(File file) {
        _showDirectory(file, 0);
    }

    //level为目录所在层级
    private static void _showDirectory(File file, int level) {
        if (file.isDirectory()) {
            for (int i = 0; i < level - 1; i++) {
                System.out.print("\t");
            }
            System.out.println(file.getName());
            for (File temp : file.listFiles()) {
                _showDirectory(temp, level + 1);
            }
        } else {
            for (int i = 0; i < level - 1; i++) {
                System.out.print("\t");
            }
            System.out.println(file.getName());
        }
    }
}
阅读更多

没有更多推荐了,返回首页