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

来源:互联网 发布:炉石传说 朝神 知乎 编辑:程序博客网 时间:2024/06/10 14:52

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

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 blocke.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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);    }    @Overridepublic boolean accept(File dir, String name) {// TODO Auto-generated method stubreturn 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 0
原创粉丝点击