• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

【原创】Copy源文件,并去掉注释

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 26次 | 进入软件测试论坛讨论

领测软件测试网
/**  DelComment.java * *   Copy this file to Copy.java,  *   and get rid of the comments. * *  Author:李文雄。033534026 *  Class :电子政务 * *   Time:04-08-2005 * *  可以把一些//和/*嵌套的注释也能够去掉, *   另外也可以区别出那些是字符串的字符,而不是注释。 *   完成的功能比较好 * */package Class;import java.io.*;

public class DelComment{  private static void outLine(BufferedWriter out,String line ,int n){   // Used to write to the outfile    String s = "第" + n + "行:";  line = s + line;  try{   out.write(line);   out.newLine();  }  catch(IOException e){   e.printStackTrace();  }  } public static void main(String[] args){ //Main Function   int n = 0;   // note the line number    String line = ""; //Used to load the string  String temp = "";    int cmtIndex0 = -1;  //用来标记 字符串引号 "  int cmtIndex4 = -1;     int cmtIndex1 = -1;  //用来标记 注释符号 //   int cmtIndex2 = -1;  //用来标记 注释符号 /* */      boolean isCmt1 = false;  //用来标记 注释符号 //   boolean isCmt2 = false;  //用来标记 注释符号 /* */       try{   BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("DelComment.java")));   //BufferedWriter out = new BufferedWriter(new FileWriter("copy.java"));   BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy.java")));      while((temp = in.readLine()) != null){          cmtIndex2 = temp.indexOf("*/");          if((cmtIndex2 != -1) && isCmt2){      isCmt2 = false;      line = temp.substring(cmtIndex2+2);      n++;      outLine(out,line,n);      continue;     }          if(isCmt2) continue;          cmtIndex0  = temp.indexOf("\"");  //出现左引号"的位置          cmtIndex1 = temp.indexOf("//");      cmtIndex2 = temp.indexOf("/*");           cmtIndex4 = temp.indexOf("\"",cmtIndex0+1);  //出现右引号"的位置          if( !((cmtIndex0 < cmtIndex1)  &&   (cmtIndex1 < cmtIndex4))  ){       //  "//" 不是在两个引号之间            if(cmtIndex1 != -1){       if( (cmtIndex1 < cmtIndex2) || (cmtIndex2 == -1))         if(cmtIndex1 > 0){         line = temp.substring(0,cmtIndex1);         n++;         outLine(out,line,n);         continue;        }      }      }      if( !((cmtIndex0 < cmtIndex2)  &&   (cmtIndex2 < cmtIndex4))  ){         //  "/*" 不是在两个引号之间             if(cmtIndex2 != -1){       if(cmtIndex2 > 0){        line = temp.substring(0,cmtIndex2);        n++;        outLine(out,line,n);       }       isCmt2 = true;       continue;      }     }      n++;     outLine(out,temp,n);    }     in.close();    out.close();  }catch(EOFException e){} //catch the exception    catch(IOException e){   e.printStackTrace();   }     catch(Exception e){   e.printStackTrace();  }  //********* Those below just for test of "Get rid of comment"  String str1 = "//   This is just to test about (//)";  String str2 = "/*   This is just to test about (/*  */)  */";  System.out.println(str1);  System.out.println(str2); }}



==>输出结果:

http://blog.csdn.net/horisly/archive/2005/04/08/340159.aspx

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网