COPY文件,过滤注释.

来源:互联网 发布:装修精美淘宝店 编辑:程序博客网 时间:2024/05/16 18:38

package com.sinosoft.lis.scm.socket;

 

import java.io.*;

 

public class FileFitherCopy {

 private static File source, target;

 private static void copyWithoutCommentsAndBlank(File source, File target) {
  try {
   BufferedReader br = new BufferedReader(new FileReader(source));
   BufferedWriter bw = new BufferedWriter(new FileWriter(target));

   String text;

   while ((text = br.readLine()) != null) {
    if (text.matches("//s*/n?/t*"))
     continue;

    if (text.matches("//s*/t*.*//.*/n?")) { //处理行注释"//"与代码混合的情况
     text = text.split("//")[0];

     if ("//s*/t*".equals(text) | "".equals(text))
      continue;
    }

    if (text.startsWith("/*") | text.startsWith("*")
      | text.endsWith("*/")) { //处理段落注释"/*...*/",我默认段落注释是不会夹杂在代码中的
     continue;
    }
    System.out.println(text);
    bw.write(text);
    bw.newLine();
   }
   bw.close();
  } catch (IOException ioException) {
   System.err.println("Error!");
  }
 }

 public static void main(String args[]) {
  source = new File("d:/Server.java");
  target = new File("e:/B.java");
  
  copyWithoutCommentsAndBlank(source, target);
  System.out.println("/nFINISH!");
 }
}

原创粉丝点击