作业一 目标二 从命令行输入一个参数(指定目录或文件),输出该目录下指定类型文件(.cs, .java)的个

时间:2021-05-28 12:50:58
import java.io.IOException;

public class Code {
    public static void main(String args[]) throws IOException{
    			new  SystemIn();
    }
}
import java.io.*;

public class SystemIn {
	  static InputStream in=System.in;      //键盘录入
	  static StringBuilder sb=new StringBuilder();   //建立缓冲区	
	 
		  SystemIn() throws IOException{
			  String file;
			  String type;
			  System.out.println("请输入文件目录");
			  file=ReadIn();
			  sb.delete(0,sb.length());	 
			  System.out.println("请输入文件类型");	  
			  type=ReadIn();
			  
			  Filecount count=new  Filecount(file,type);
		  
		  
		  
	  }
	  public static String ReadIn() throws IOException{
		  {
			  String name;
			  while(true){
				  int ch=in.read();
				  if(ch=='\r')
					  continue;
				  if(ch=='\n')
				  {
					   name=sb.toString();				
						   break;
				  }
				  else{
					  sb.append((char)ch);
				  }
			  }
		  }
		return sb.toString();  
	  }
	  
}

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class Filecount {
	    String file;
	    String type;
	    
	    int filenum;
	    
		Filecount(String file,String type) throws IOException{
			this.file=file;
			this.type=type;
			
			System.out.println(file);
			File dir = new File(file);
			
			List<File> list = new ArrayList<File>();
			count(dir,list);
	
		}
		
		public void count(File dir,List<File> list) throws IOException{                       // 计算目录下有类型文件个数
			File[] files = dir.listFiles();
			filenum=0;
			for(File file : files)
			{   
					if(file.getName().endsWith(type)){
							filenum++;
							System.out.println(file.getName());
							new SumCode(file);			
					}		
				}
			System.out.println(type+"类型文件个数:"+filenum);
			}
		}


import java.io.*;


public class SumCode {
	
	 int BlankLine=0;
	 int CommentLine=0;
	 int CodeLine=0;
	 int SumLine=0;
	 
	 String regex="^[\\s&&[^\\n]]*$";

	       SumCode(File file) throws IOException {
		    	   boolean flag=false;
			       BufferedReader br;
	
		    	   br=new BufferedReader(new FileReader(file));          
		    	   String line=null;
		    	   
		    	   while((line=br.readLine())!=null){                             
		    		   SumLine++;
		    			if (line.matches("^[\\s&&[^\\n]]*$")) { 
							BlankLine++; 
						} else if (line.startsWith("/*") && !line.endsWith("*/")) { 
							CommentLine++; 
							flag = true; 
						} else if (flag==true) { 
							CommentLine++; 
							if (line.endsWith("*/")) { 
								flag = false; 
							} 
						} else if (line.startsWith("//")) { 
								CommentLine++; 
						} else { 
							CodeLine++; 
						} 
		    	   }
			    	   
		       br.close();
		       
		       	System.out.println("空行数:"+BlankLine);
				System.out.println("注释行数:"+CommentLine);
				System.out.println("代码行数:"+CodeLine);
			    System.out.println("总行数:"+SumLine);
     
	}

}