- package com.fwj.reitheima.day20;
- import java.io.File;
- public class FileDemo3 {
- public static void main(String[] args) throws Exception {
- File dir = new File("d:\\");
- if (!dir.exists()) {
- System.out.println(dir.mkdirs());
- }
- showDir(dir, 0);
- }
- private static String getlevel(int level) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < level; i++) {
- sb.insert(0, "|--");
- }
- return sb.toString();
- }
- private static void showDir(File dir, int level) {
- // TODO Auto-generated method stub
- System.out.println(getlevel(level) + dir.getName());
- level++;
- File[] files = dir.listFiles();
- for (File f : files) {
- if (f.isDirectory()) {
- showDir(f, level);
- } else {
- System.out.println(getlevel(level) + f.getName());
- }
- }
- }
- }
复制代码 这是异常
Exception in thread "main" java.lang.NullPointerException
at com.fwj.reitheima.day20.FileDemo3.showDir(FileDemo3.java:28)
at com.fwj.reitheima.day20.FileDemo3.showDir(FileDemo3.java:30)
at com.fwj.reitheima.day20.FileDemo3.main(FileDemo3.java:12)
没有想明白
|