统计.java文件的个数
package file;
import java.io.*;
public class ListFile
{
private int count=0;
public int getCount()
{
return count;
}
public void listDir(String url)
{
File file=new File(url);
if(!file.exists())
{
System.out.println("此路径不存在");
return ;//程序不在就行执行
}
if(file.isDirectory())
{
//遍历目录下的所有文件及目录
File[] files=file.listFiles();
for(int i=0;i
{
//递归调用
this.listDir(files[i].getAbsolutePath());
}
}
else
{
//System.out.println(file.getAbsolutePath());
//过滤文件,只输出.Java后缀的文件
if(file.getAbsolutePath().endsWith(".java"))
{
System.out.println(file.getAbsolutePath());
Reader r=null;
BufferedReader br=null;
try
{
r=new FileReader(file);//读文件
br=new BufferedReader(r);//按行读文件
while(br.readLine()!=null)//统计文件的行数
{
this.count++;
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String args[])
{
ListFile lf=new ListFile();
lf.listDir("E:/可移动磁盘 备份/java作业");
System.out.println("恭喜你!目前为止,你已经写了"+lf.getCount()+ "行代码了");
System.out.println("继续努力!未来属于你");
}
}