public static void main(String[] args) {
File f = new File("d:/A");
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();
for(int i=0; i<childs.length; i++) {
System.out.println(preStr + childs[i].getName());
if(childs[i].isDirectory()) {
tree(childs[i], level + 1);
}
}
}

}

解决方案 »

  1.   

    f.listFiles()为空的,没有文件吧
      

  2.   

    File[] childs = f.listFiles();
    判断下 childs 是否为空再用吧
      

  3.   

    上面写错了抱歉,应该说你的D盘下没有那个A文件吧,或者说判断下 childs 是否为空再用吧
    File[] childs = f.listFiles();
      

  4.   

    d盘没有这个文件吧,楼主,在往下查找之前是不是先判断一下比较好,即if(!f.isDirectory()) return;//判断放在查找前
    File[] childs = f.listFiles();
    for(int i=0; i<childs.length; i++) {
    listChilds(childs[i], level + 1);
    }
      

  5.   


    lz可以先加个判断:
    if (f.exists()) {
    //...
    }else {
    f.create();
    }
    之类的