以D盘为目标路径,打印了几行就提示空指向异常其他盘几乎就直接提示空指向异常了
public class JavaTest26_04{
public static void main(String args[]){
JavaTest26_04 j = new JavaTest26_04();
j.loop("d:\\");
}
public void loop(String lj){
String list[] = null;
File f = new File(lj);
if(f.isDirectory()){
list = f.list();
for(int i=0;i<list.length;i++){
loop(lj + "\\" + list[i]);
}
}
else{
System.out.println(lj);
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
list = f.list();
这一句中list可能为null。看一下File里面的list函数的文档,上面有写:
@return An array of strings naming the files and directories in the
directory denoted by this abstract pathname. The array will be
empty if the directory is empty. Returns {@code null} if
this abstract pathname does not denote a directory, or if an
I/O error occurs.
当File不是目录或者发生I/O错误的时候,会返回null。
你这里应该是发生I/O错误了,所以下一个语句会报空指针异常。
PS:可能是你的D盘下有一个隐藏的文件夹System Volume Information,访问这个文件夹时会报错。