正则表达式--统计代码量

前端之家收集整理的这篇文章主要介绍了正则表达式--统计代码量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6.  
  7. public class MainClass {
  8.  
  9. static long normalline = 0;
  10. static long commetline = 0;
  11. static long writeline = 0;
  12.  
  13. public static void main(String[] args) {
  14.  
  15. File file = new File("D:\\java_projects\\MainTestClass\\src");
  16. File[] allfiles = file.listFiles();
  17. for (File child : allfiles) {
  18. if (child.getName().matches(".*\\.java$")) {
  19. parse(child);
  20. }
  21. }
  22. System.out.println("normalline="+normalline);
  23. System.out.println("commentline="+commetline);
  24. System.out.println("whiteline="+writeline);
  25. }
  26. /* zhu shi */
  27. /* * zhu shi */
  28. private static void parse(File child) {
  29. boolean comment=false;
  30. BufferedReader bufferedReader=null;
  31. try {
  32. bufferedReader = new BufferedReader(new FileReader(child));
  33. String line="";
  34. while ((line=bufferedReader.readLine())!=null) {
  35. line = line.trim();
  36. if (line.matches("^[\\s&&[^\\n]]*$")) {
  37. writeline++;
  38. }else if (line.startsWith("/*") && !line.endsWith("*/")) {
  39. commetline++;
  40. comment=true;
  41. }else if (comment==true) {
  42. commetline++;
  43. if (line.endsWith("*/")) {
  44. comment=false;
  45. }
  46. }else if (line.startsWith("//")) {
  47. commetline++;
  48. }else if (line.startsWith("/*")&&line.endsWith("*/")) {
  49. commetline++;
  50. }else {
  51. normalline++;
  52. }
  53. }
  54. } catch (FileNotFoundException e) {
  55. e.printStackTrace();
  56. }catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. }

output:

  1. normalline=54
  2. commentline=4
  3. whiteline=4

猜你在找的正则表达式相关文章