本帖最后由 陌路行者 于 2013-7-9 18:09 编辑
- import java.io.*;
- class FileDemo2
- {
- public static void main(String[] args)
- {
- File f = new File("C:\\");
- dirShow(f,0);
- }
- public static void dirShow(File dir,int level)
- {
- System.out.println(getLevel(level)+dir.getName());
- level++;
- File[] files = dir.listFiles();
- for(int i=0;i<files.length;i++)
- {
- if(files[i].isDirectory())
- dirShow(files[i],level);
- else
- System.out.println(getLevel(level)+files[i]);
- }
- }
- public static String getLevel(int level)
- {
- StringBuilder sb = new StringBuilder();
- sb.append("|--");
- for(int i=0;i<level;i++)
- {
- sb.insert(0,"| ");
- }
- return sb.toString();
- }
- }
复制代码 为啥子报空指针?
|
|