现在想做个把文件的文档注释删除的东西?不知道java怎样用正则表达式来匹配,请各位帮忙
5条回答 默认 最新
- turing-complete 2011-04-28 14:52关注
写了个例子,你可以参照下
[code="java"]
package iteyeQuestions;/*
- java怎样用正则表达式匹配文档注释,如“斜杠**@date 2012-1-2*斜杠”?
- 现在想做个把文件的文档注释删除的东西?不知道java怎样用正则表达式来匹配,请各位帮忙 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;public class RidNotation {
//待处理代码文档输入流 private BufferedReader br; //待处理代码文档输出流 private PrintWriter pw; //构造方法 public RidNotation(String inputUrl,String outputUrl) throws IOException{ this.br= new BufferedReader(new FileReader(new File(inputUrl))); this.pw= new PrintWriter(new BufferedWriter(new FileWriter(outputUrl)));; } //去文档型注册的方法 public void ridDocType() throws IOException{ String temp=null; boolean isNotation=false; while (this.br.ready()) { temp=this.br.readLine(); int start=0; int end=0; if(isNotation){ if((end=temp.indexOf("*/"))==-1){ continue; }else{//找到注释结束标志 int anotherStart=0; if((anotherStart=temp.substring(end).indexOf("/**"))==-1){ isNotation=false;//此后为代码段 this.pw.println(temp.substring(end+2));//把本行剩余的代码输出 }else{//又在本行内找到另一个开始标志,输出中间代码 this.pw.println(temp.substring(end+2,anotherStart)); } } }else{ if((start=temp.indexOf("/**"))==-1){ this.pw.println(temp); }else{//找到注释开始标志 if((end=temp.substring(start).indexOf("*/"))==-1){ isNotation=true;//此后为注释段 this.pw.println(temp.substring(0,start)); }else{//此行内同时含有注释结尾标志 this.pw.println(temp.substring(0,start)+temp.substring(end+2)); } } } } this.br.close(); this.pw.close(); } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //测试代码 new RidNotation("RidNotation.java","outttt.txt").ridDocType(); }
}
[/code]本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报