本帖最后由 杨庆雷 于 2014-6-28 00:27 编辑
- package IO;
- import java.io.File;
- public class FileDemo3 {
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- File f = new File("c:\\Windows");
- showDir(f,0);
- }
-
- public static void showDir(File dir,int level){
- System.out.println(getLevel(level)+dir.getName());
- level++;
- File[] files =dir.listFiles();
- for(int x=0;x<files.length;x++){
-
- if(files[x].isDirectory()){
- showDir(files[x],level);
-
- }else{
- System.out.println(getLevel(level)+files[x]);
- }
- }
- }
-
- public static String getLevel(int level){
- StringBuilder sb = new StringBuilder();
- for(int x=0;x<level;x++){
-
- sb.append("-----");
- }
-
- return sb.toString();
- }
- }
复制代码- Exception in thread "main" java.lang.NullPointerException
- at IO.FileDemo3.showDir(FileDemo3.java:17)
- at IO.FileDemo3.showDir(FileDemo3.java:20)
- at IO.FileDemo3.showDir(FileDemo3.java:20)
- at IO.FileDemo3.main(FileDemo3.java:10)
复制代码 有的目录会有空指针 换一个目录可能就没空指针 怎么回事这个
|