需求:列出指定目录下文件或者文件夹,包含子目录中的内容。也就是列出指定目录下的所有内容。
代码如下
import java.io.*;
class DiGui_1
{
public static void main(String[]args)
{
File dir=new File("e:\\");
showDir(dir);
}
public static void showDir(File dir)
{
System.out.println(dir);
File[]files=dir.listFiles();
for (int x=0;x<files.length ;x++ )
{
if(files[x].isDirectory())
showDir(files[x]);
else
System.out.println(files[x]);
}
}
}
结果怎么是
D:\java\zuoye\file>java DiGui_1
e:\
e:\$RECYCLE.BIN
e:\$RECYCLE.BIN\S-1-5-21-1067235349-3175385825-1744776708-1000
e:\$RECYCLE.BIN\S-1-5-21-1067235349-3175385825-1744776708-1000\desktop.ini
e:\$RECYCLE.BIN\S-1-5-21-2872404224-3370634769-2218778021-500
e:\$RECYCLE.BIN\S-1-5-21-2872404224-3370634769-2218778021-500\desktop.ini
e:\360Rec
e:\360Rec\20120526
e:\360Rec\20120526\2033C.vir
e:\RECYCLER
e:\RECYCLER\S-1-5-18
e:\RECYCLER\S-1-5-18\desktop.ini
e:\RECYCLER\S-1-5-18\INFO2
e:\RECYCLER\S-1-5-21-1220945662-1500820517-1417001333-500
e:\RECYCLER\S-1-5-21-1220945662-1500820517-1417001333-500\desktop.ini
e:\RECYCLER\S-1-5-21-1220945662-1500820517-1417001333-500\INFO2
e:\RECYCLER\S-1-5-21-3027779725-3314400031-2191039975-500
e:\RECYCLER\S-1-5-21-3027779725-3314400031-2191039975-500\desktop.ini
e:\RECYCLER\S-1-5-21-3027779725-3314400031-2191039975-500\INFO2
e:\RECYCLER\S-1-5-21-343818398-117609710-682003330-500
e:\RECYCLER\S-1-5-21-343818398-117609710-682003330-500\desktop.ini
e:\RECYCLER\S-1-5-21-343818398-117609710-682003330-500\INFO2
e:\RECYCLER\S-1-5-21-448539723-606747145-1177238915-500
e:\RECYCLER\S-1-5-21-448539723-606747145-1177238915-500\desktop.ini
e:\RECYCLER\S-1-5-21-448539723-606747145-1177238915-500\INFO2
e:\RECYCLER\S-1-5-21-725345543-838170752-2146829589-500
e:\RECYCLER\S-1-5-21-725345543-838170752-2146829589-500\desktop.ini
e:\RECYCLER\S-1-5-21-725345543-838170752-2146829589-500\INFO2
e:\System Volume Information
Exception in thread "main" java.lang.NullPointerException
at DiGui_1.showDir(myfile.java:179)
at DiGui_1.showDir(myfile.java:182)
at DiGui_1.main(myfile.java:173)
D:\java\zuoye\file>
然后我改成"e:\\邵阳文件"。都不会出现问题。
会不会是e盘上的隐藏的系统文件,是递归出现了异常。
|
|