public class Main { public static void main(String[] args) throws IOException { File file = new File("D:\\workspaces\\HelloWorld\\out"); String str = ""; System.out.println(file.getName()); printChildFile(file, str); } static void printChildFile(File file, String str) { if (file.isDirectory()) { str = str + " "; File[] files = file.listFiles(); if (files != null) { for (File f : files) { if (f.isFile()) { System.out.println(str + f.getName()); } else if (f.isDirectory()) { System.out.println(str + f.getName()); printChildFile(f, str); } } } } } }
文件夹输出修改:
System.out.println(str + f.getName() + "/");
else if (f.isDirectory()) { System.out.println(str + f.getName() + "/"); printChildFile(f, str);//需要用到递归调用,否则子目录中的文件无法遍历到
Sign in to make a reply
好先生的菜