不包含文件夹
import java.io.*;
public class CountFiles {
private static int i=0;
public static void count(File f){
if(f.isDirectory()){
File[] files=f.listFiles();
for(int j=0;j <files.length;i++)
//System.out.println(files[i].toString());
count(files[j]);
}
else i++;
}
public static void main(String[] args){
if(args.length!=1)
System.out.println( "please input the file name ");
File f=new File(args[0]);
count(f);
System.out.println( "The count of this directory is "+i);
}
}
这方法为什么行不通啊。。。。。
------解决方案--------------------
import java.io.*;
public class CountFiles {
private static int i=0;
public static void count(File f){
if(f.isDirectory()){
File[] files=f.listFiles();
for(int j=0;j <files.length;j++,i++)
//System.out.println(files[i].toString());
count(files[j]);
}
else i++;
}
public static void main(String[] args){
if(args.length!=1)
System.out.println( "please input the file name ");
File f=new File(args[0]);
count(f);
System.out.println( "The count of this directory is "+CountFiles.i);
}
}
------解决方案--------------------
File f=new File(args[0]);
楼上的我的编译器到这里报了错!!
Exception in thread "main " java.lang.ArrayIndexOutOfBoundsException: 0
at CountFiles.main(CountFiles.java:18)
这是什么错误呢??