<span style="color:#ff6666;">在控制台中,输入一个路径,然后进行各种行数的读取,至于判断的是不是非法的路径的,我忘记写在上面了。只要在代码上加上if()判断就可以了。而至于判断是什么类型的</span>
<span style="color:#ff6666;">语言编写的则可用indexof()来检查</span>
public class Reader2 {
/**
* show里面先查看文件夹,看有多少的文件,然后再查看里面的文件内容
*/
public static void show(File file) throws IOException{
if (file!=null) {
if (file.isDirectory()) {
File f[]=file.listFiles();
if(f!=null){
for(int i=0;i<f.length;i++){
show(f[i]);
}
}
}else {
System.out.println(file);
int blank=0;
int note =0;
int code=0;
int sum=0;
//1,读取文件
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
//2,读去里面的文件的具体行
BufferedReader br2=new BufferedReader(new FileReader(file));
String line=null;
while((line=br2.readLine())!=null){
if(line.matches("^[\\s&&[^\\n]]*$")){
blank++;
}else if (line.startsWith("//")) {
note++;
}
else{
code++;
}
}
sum=blank+note+code;
br1.close();
br2.close();
System.out.println("空行:"+blank);
System.out.println("注释行:"+note);
System.out.println("代码行:"+code);
System.out.println("总行:"+sum);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader bReader =new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入你所要操作的文件:");
String pathname=bReader.readLine();
File file=new File(pathname);
show(file);
}
}
<img src="https://img-blog.csdn.net/20150408155926775?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDY3NDA0OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />