Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1601628
  • 博文数量: 43
  • 博客积分: 169
  • 博客等级: 入伍新兵
  • 技术积分: 1162
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-08 15:35
文章分类

全部博文(43)

文章存档

2021年(1)

2019年(4)

2016年(6)

2015年(8)

2013年(19)

2012年(5)

分类: Java

2013-04-07 20:38:17

读取java文件注释


    通过渐进法读取并输出java文件的所有注释/* */和//

点击(此处)折叠或打开

  1. package com.cn.fanjg.regex;

  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;

  8. /**
  9.  * @function:打印java文件中的所有注释
  10.  * com.cn.fanjg.regex
  11.  * @author fanjg
  12.  * @date : 2013-4-7 下午06:57:20
  13.  */
  14. public class PrintComments {
  15.     public static void main(String[] args) {
  16.         try {
  17.             FileReader freader = new FileReader("./ThreadMain.java");
  18.             BufferedReader breader = new BufferedReader(freader);
  19.             StringBuilder sb = new StringBuilder();
  20.             try {
  21.                 String temp = "";
  22.                 /**
  23.                  * 读取文件内容,并将读取的每一行后都不加\n
  24.                  * 其目的是为了在解析双反斜杠(//)注释时做注释中止符
  25.                  */
  26.                 while((temp=breader.readLine())!= null)
  27.                 {
  28.                     sb.append(temp);
  29.                     sb.append('\n');
  30.                 }
  31.                 String src = sb.toString();
  32.                 /**
  33.                  * 1、做/* 注释的正则匹配
  34.                  *


  35.                  *     通过渐进法做注释的正则匹配,因为/*注释总是成对出现
  36.                  * 当匹配到一个/*时总会在接下来的内容中会首先匹配到"*\\/",
  37.                  * 因此在获取对应的"*\\/"注释时只需要从当前匹配的/*开始即可,
  38.                  * 下一次匹配时只需要从上一次匹配的结尾开始即可
  39.                  * (这样对于大文本可以节省匹配效率)——
  40.                  * 这就是渐进匹配法
  41.                  *


  42.                  * */
  43.                 Pattern leftpattern = Pattern.compile("/\\*");
  44.                 Matcher leftmatcher = leftpattern.matcher(src);
  45.                 Pattern rightpattern = Pattern.compile("\\*/");
  46.                 Matcher rightmatcher = rightpattern.matcher(src);
  47.                 sb = new StringBuilder();
  48.                 /**
  49.                  * begin 变量用来做渐进匹配的游标 {@value}
  50.                  * 初始值为文件开头
  51.                  * **/
  52.                 int begin = 0;
  53.                 while(leftmatcher.find(begin))
  54.                 {
  55.                     rightmatcher.find(leftmatcher.start());
  56.                     sb.append(src.substring(leftmatcher.start(), rightmatcher.end()));
  57.                     /** 为输出时格式的美观 **/
  58.                     sb.append('\n');
  59.                     begin = rightmatcher.end();
  60.                 }
  61.                 System.out.println(sb.toString());
  62.                 /**
  63.                  * 2、对//注释进行匹配(渐进匹配法)
  64.                  * 匹配方法是 // 总是与 \n 成对出现
  65.                  * */
  66.                 begin = 0;
  67.                 Pattern leftpattern1 = Pattern.compile("//");
  68.                 Matcher leftmatcher1 = leftpattern1.matcher(src);
  69.                 Pattern rightpattern1 = Pattern.compile("\n");
  70.                 Matcher rightmatcher1 = rightpattern1.matcher(src);
  71.                 sb = new StringBuilder();
  72.                 while(leftmatcher1.find(begin))
  73.                 {
  74.                     rightmatcher1.find(leftmatcher1.start());
  75.                     sb.append(src.substring(leftmatcher1.start(), rightmatcher1.end()));
  76.                     begin = rightmatcher1.end();
  77.                 }
  78.                 System.out.println(sb.toString());
  79.             } catch (IOException e) {
  80.                 System.out.println("文件读取失败");
  81.             }finally
  82.             {
  83.                 breader.close();
  84.                 freader.close();
  85.             }
  86.         } catch (FileNotFoundException e) {
  87.             System.out.println("文件不存在");
  88.         }catch(IOException e)
  89.         {
  90.             System.out.println("文件读取失败");
  91.         }
  92.     }

  93. }


阅读(8170) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~