klExceptioninthread"main"java.lang.NullPointerExceptionatio9.FileList.tree(FileList.java:20)atio9.FileList.main(FileList.java:8)代码:packageio9;importjava.io.*;publiccla...
klException in thread "main" java.lang.NullPointerException at io9.FileList.tree(FileList.java:20) at io9.FileList.main(FileList.java:8)代码:package io9;import java.io.*; public class FileList { public static void main(String[] args) { File f = new File("d:/dzh2/gh/kl"); System.out.println(f.getName()); tree(f, 1); } // 递归方法 private static void tree(File f, int level) { String preStr = ""; for (int i = 0; i < level; i++) { preStr += "-"; } File[] childs = f.listFiles(); //listFiles()返回目录下的所有文件. for (int i = 0; i < childs.length; i++) { System.out.println(preStr + childs[i].getName()); if (childs[i].isDirectory()) { //isDirectory()判断是否为目录 tree(childs[i], level + 1); } } } }
展开