import java.io.File;
import java.io.FilenameFilter;
public class Demo {
public static void main(String[] args){
File dir = new File("d:");
showDir(dir);
}
public static void showDir(File dir)
{
System.out.println(dir);
File[] files = dir.listFiles();
for(int i=0; i<files.length; i++){
if(files[i].isDirectory())
showDir(files[i]);
else
{
String str = files[i].toString();
if(str.endsWith(".mp3"))
System.out.println(files[i]);
}
}
}
}
代码如上,运行的结果是:
d:\360SoftMove\SoftMoveConfig\我的音乐
d:\360SoftMove\SoftMoveConfig\收藏夹
d:\360SoftMove\SoftMoveConfig\桌面
d:\360SoftMove\SoftMoveConfig\用户临时文件夹
d:\aa
d:\CloudCache
d:\CloudCache\bfcapp
d:\CloudCache\bfcapp\link
d:\IP代理_2.88
d:\Media
d:\System Volume Information
Exception in thread "main" java.lang.NullPointerException
at For20_7.Demo.showDir(Demo.java:17)
at For20_7.Demo.showDir(Demo.java:19)
at For20_7.Demo.main(Demo.java:10)
说空指针错误,我特意传个空文件夹的对象,都不报错,老是扫描整个d盘就出错,求指点 |