猿问

正则表达式统计怎么统计空行、代码行、注释

用这个程序测代码行、空行和注释行,老是显示都为0行,麻烦看看哪里错了啊
  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. public class CodeCounter {

  7. static long codeLines = 0;

  8. static long commentLines = 0;

  9. static long whiteLines = 0;

  10. public static void main(String[] args) {

  11. File f = new File("D:\\Workspaces\\MyEclipse Professional 2014\\DOMTest\\src\\question");

  12. File [] codeFiles = f.listFiles();

  13. for (File file : codeFiles) {

  14. if (file.getName().matches(".* \\.java$")) {

  15. parse(file);

  16. }

  17. }

  18. System.out.println(codeLines);

  19. System.out.println(commentLines);

  20. System.out.println(whiteLines);

  21. }


  22. private static void parse(File f) {

  23. BufferedReader br = null;

  24. boolean comment = false;

  25. try {

  26. br = new BufferedReader(new FileReader(f));

  27. String line = "";

  28. while ((line = br.readLine()) != null) {

  29. line = line.trim();

  30. if (line.matches("^[\\s&&[^\\n]]*$")) {

  31. whiteLines++;

  32. } else if (line.startsWith("/*") && line.endsWith("*/")) {

  33. commentLines++;

  34. comment = true;

  35. } else if (true == comment) {

  36. commentLines ++;

  37. if (line.endsWith("*/")) {

  38. comment = false;

  39. }

  40. } else {

  41. codeLines ++;

  42. }

  43. }

  44. } catch (FileNotFoundException e) {

  45. e.printStackTrace();

  46. } catch (IOException e) {

  47. e.printStackTrace();

  48. } finally {

  49. if (br != null) {

  50. try {

  51. br.close();

  52. br = null;

  53. } catch (IOException e) {

  54. e.printStackTrace();

  55. }

  56. }

  57. }

  58. }

  59. }

fenkapian
浏览 1940回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答