JAVA实现简单的计算行数程序

程序实现了统计行数,空白行数和注释数,在计算注释的功能上没有实现代码和注释在同一行这种情况的注释统计,感觉思路是读取行的内容,再判断“//”的存在,不过情况好像挺复杂的,懒得去实现了,各位有能力实现的求指教。下面贴代码:

public class Counter {
    //这个方法可有可无,在我另外的例子用到了^_^
	public boolean isJavaFile(String path) {
		if (path != null && path.length() != 0 && path.matches("[\\s\\S]*\\.java")) {
			    return true;
		 }
         return false;
	}
    

	public Map<String, String> openDir(String path, String type) {
		int docNum = 0;
		Map<String, String> map = new HashMap<String, String>();
		File file = new File(path);
		if(!file.isDirectory() && !file.isFile()){
			map.put("erro", "不是一个有效的文件或路径");
			return map;
		}
		if (file.isFile()) {
			map.put("file", file.getPath());
            return map;
		}
		File[] list = file.listFiles(new MyFileFilter("[\\s\\S]*\\." + type));
		for (File item : list) {
			map.put(item.getName(), item.getPath());
			docNum++;
		}
		map.put("num", "共有"+docNum+"个"+type+"文件");
		return map;
	}

	public void count(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		String tempString = null;
		int line = 0;// 总行数
		int space = 0;// 空白行数
		int explain = 0;// 解释行数
		int code = 0;// 代码行数
		boolean flag = true;
		try {
			reader = new BufferedReader(new FileReader(file));
			while ((tempString = reader.readLine()) != null) {

				line++;
				if (tempString.length() == 0 && flag) {
					space++;
				} else if (tempString.trim().startsWith("//") && flag) {
					explain++;
				} else if (tempString.trim().startsWith("/*")) {
					explain++;
					flag = false;
				} else if (tempString.trim().startsWith("*/") && !flag) {
					explain++;
					flag = true;
				} else if (!tempString.trim().startsWith("/*") && !flag) {
					explain++;
				}

			}
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		code = line - space - explain;
		System.out.println("总行数:" + (line) + "\n" + "空白行数:" + (space) + "\n"
				+ "注释行数:" + explain + "\n" + "代码行数:" + code);

	}

}
下面是一个文件类型的Filter,根据传入的正则表达式,过滤掉不符合参数的文件。

public class MyFileFilter implements FilenameFilter {
    private Pattern pattern;
    public MyFileFilter(String regex){
    	pattern = Pattern.compile(regex);
    }
    
	@Override
	public boolean accept(File dir, String name) {
		// TODO Auto-generated method stub
		return pattern.matcher(name).matches();
	}

}
再看实现类,类名随便起的,将就一下。

public class Three {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		   Counter co = new Counter();
		   
	       System.out.println("请输入文件路径");
	       Scanner sc = new Scanner(System.in);
		   String path = sc.nextLine();
		   String type = "java";
		   
           
           //"C:\\Users\\lenovo\\Desktop\\jdyvpn"
           Map<String,String>map = co.openDir(path,type);
	       String value = null;
	       while(map.containsKey("erro")){
	    	   System.out.println(map.get("erro")+",请输入正确的路径");
		       map.clear();
	    	   sc = new Scanner(System.in);
			   path = sc.nextLine();
			   map = co.openDir(path,type);
	       }
	       File file = new File(path);
	       if(file.isDirectory()){
	       System.out.println("请输入文件类型");
	       type = sc.nextLine();
	       map = co.openDir(path,type);
	       while(map.isEmpty()){
	    	   System.out.println("没有该类型的文件,请输入确实存在的文件类型");
		       map.clear();
	    	   sc = new Scanner(System.in);
			   type = sc.nextLine();
			   map = co.openDir(path,type);
	       }
	       }
	       if(map.containsKey("file")){
		       co.count(map.get("file"));
	       }else{
	      System.out.println(map.get("num"));
	 	  map.remove("num");
          for (String key:map.keySet()){
		  value = map.get(key);
		  System.out.println(key);
		  co.count(value);
	   }
  }
}
}

运行截图如下:





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值