删除java的注释信息的源码

删除java的注释信息的源码
 
import java.util.regex.*;
import java.io.*;
import util9.*;
//某些java文件注释信息过多从而影响了可读性。利用此类可解决这个问题。
//使用方法略。
public class DelNoteInJavaFile {
  static BufferedReader br;
  static BufferedWriter bw;
  static String suffix=".noNote.java";
  public DelNoteInJavaFile()
  {
          String message="input file name:";
          String   ret=Read9.stringFromControl(message);
          if(inJava(new File(ret)))System.out.println("successed");
          else  System.out.println("failed");
  }
  public DelNoteInJavaFile(String f)
  {
          if(inJava(new File(f)))System.out.println("successed");
          else  System.out.println("failed");
  }
  public DelNoteInJavaFile(File f)
  {
          if(inJava(f)) System.out.println("successed");
          else  System.out.println("failed");
  }
  public static boolean check(File f)
  {
    if (!f.exists()) {
      System.err.println("wrong1.file not exists");
      return false;
    }
    if (!f.isFile()) {
      System.err.println("wrong2.abstract file is not a file");
      return false;
    }
    return true;
  }
  public static boolean inJava(File f) {
    if(check(f)==false ) return false ;
    File ff = new File(f.getName() + suffix);
    if (ff.exists()) {
      System.out.println("wrong3");
      return false;
    }
    String s, ss;
    try {
      ff.createNewFile();
      br = new BufferedReader(new FileReader(f));
      bw = new BufferedWriter(new FileWriter(ff));
      //the slash is char '/',the star is char '*'.
      Pattern slash_star_start = Pattern.compile("///*.*");//just like:/*....
      Pattern star_slash = Pattern.compile("^.*//*/");
      Pattern slash_star_both = Pattern.compile("///*.*//*/");
      Pattern slash2 = Pattern.compile("
//s{0,}//");
      //      Pattern star = Pattern.compile("
//s{0,}//*");
      Matcher mat;
      int i = 0;
      boolean note = false;
      while ( (s = br.readLine()) != null) {
        //deal with such string:... /*...*/....
        if(s.trim().length()==0) continue;
        s = s.replaceAll(slash_star_both.pattern(), "");
        //deal with:....//....
        mat = slash2.matcher(s);
        if (mat.find()&¬e==false) {
                System.out.println("s1="+s);
          s = s.substring(0, mat.start());
          if (s.trim().length() == 0)continue;
          bw.write(s);
          bw.newLine();
          continue;
        }
        //deal with:...*/...
       mat = star_slash.matcher(s);
        if (mat.find()) {
          note = false;
          System.out.println("s2="+s);
          s = s.substring(mat.end());

          if (s.trim().length() == 0) {
            continue;
          }
        }
        if (note == true) {
          continue;
        }
        //deal with:..../*....
        mat = slash_star_start.matcher(s);
        if (mat.find()) {
          note = true;
          System.out.println("s=" + s);
          s = s.substring(0, mat.start());
          System.out.println("s1=" + s + ",mat.start()=" + mat.start());
          if (s.trim().length() == 0) {
            continue;
          }

        }
        bw.write(s);
        bw.newLine();
      }
      br.close();
      bw.close();
      /*
      if (f.renameTo(new File(ff.getName() + ".bak"))) {
        System.out.println("backup file is:" + ff.getName() + ".bak");
        if( ff.renameTo(f))
        System.out.println("output file is:"+f.getName());
        else {System.out.println("wrong53");return false;}
      }
      else {
        System.out.println("wrong55");
        return false;
      }
      */
    }
    catch (IOException e) {
      e.getMessage();
      return false;
    }
    return true;
  }

  public static boolean inJava(String s) {
    return inJava(new File(s));
  }
  public static void main(String a[])
  {if(a.length==0)new DelNote9();
   else new DelNoteInJavaFile(a[0]);
  }
}
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值