【原創】Copy源檔案,並去掉注釋

來源:互聯網
上載者:User
原創
/**  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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.