Discuss / Java / overload

overload

Topic source
    static void listFiles() {
        File f = new File(".");
        System.out.println(f.getName() + "/");
        list(f, 1);
    }

    static void listFiles(String filename) {
        File f = new File(filename);
        System.out.println(f.getName() + "/");
        list(f, 1);
    }

    static void list(File f, int level) {
        for (File ft : f.listFiles()) {
            StringBuilder pre = new StringBuilder();
            for (int i = 0; i < level; i++) {
                pre.append("  ");
            }
            if (ft.isFile()) {
                System.out.println(pre + ft.getName());
            }
            if (ft.isDirectory()) {
                System.out.println(pre + ft.getName() + "/");
                list(ft, level + 1);
            }
        }
    }

  • 1

Reply