实现抽取java中的注释的代码

来源:互联网 发布:大数据时代的重要性 编辑:程序博客网 时间:2024/05/16 18:02
public class CopyJavaNo {       public static void main(String[] args) throws IOException {              copy("F:\\myeclipse\\UtilsOrTool\\src\\com\\hui\\utils\\jersey", "F:\\myeclipse\\UtilsOrTool\\src\\cn\\study\\jersey"); // 这里写好源文件夹和目的文件夹     //copy("F:\\myeclipse\\UtilsOrTool\\src\\cn\\study", "F:\\myeclipse\\UtilsOrTool\\src\\cn\\study"); // 这里写好源文件夹和目的文件夹      }          private static void copy(String srcPath, String descPath) throws IOException {          copy(new File(srcPath), new File(descPath));      }       private static void copy(File srcFile, File descFile) throws IOException {          if (srcFile.isFile()) { // 文件          //根据目标文件夹获取父级文件            File parent = descFile.getParentFile();              if (!parent.exists()) {                  parent.mkdirs(); // 创建文件夹              }              //若以。java结尾            if (srcFile.getName().endsWith(".java")) {              //copy源文件到目标位置,有处理                copyJava(srcFile, descFile);              } else {              //只是复制                copyFile(srcFile, descFile);              }          } else { // 文件夹   遍历文件夹中的。java文件            for (File file : srcFile.listFiles()) {                  // 相对路径  ,取源文件的名称                String srcPath = file.getAbsolutePath().substring(srcFile.getAbsolutePath().length());                  //复制原文件到目标位置,进行一些处理                copy(file, new File(descFile.getAbsolutePath() + srcPath));              }          }      }       private static void copyJava(File srcFile, File descFile) throws IOException {          BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(descFile)));          BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));          String line;        String trueLine;        StringBuffer sb=new StringBuffer();        while ((line = br.readLine()) != null) {          //这一句效率很高哦,string字符串的赋值精粹        trueLine=line;        //去掉这行的空格        line = line.trim();        //字符串以//开头的话    if(line.startsWith("//")){    sb.append(trueLine);    }    else if(line.startsWith("/*")&&line.endsWith("*/")){    sb.append(trueLine);    }else if(line.startsWith("/*")&&!line.endsWith("*/")){        sb.append(trueLine);    //当到了多行注释的结尾,跳出这一层循环    while((line=br.readLine())!=null){    //这一句效率很高哦,string字符串的赋值精粹            trueLine=line;    line = line.trim();    sb.append("\n").append(trueLine);    if(line.endsWith("*/")){    break;    }    }    }else {    sb.append("\n");}        }                  bw.write(sb.toString());         br.close();          bw.close();      }       private static void copyFile(File srcFile, File descFile) throws IOException {          OutputStream output = new FileOutputStream(descFile);          InputStream input = new FileInputStream(srcFile);          byte[] buffer = new byte[1024 * 4];          int n = 0;          while ((n = input.read(buffer)) != -1) {              output.write(buffer, 0, n);          }          input.close();          output.close();      }   }
参考了网上一些回答,自己用io流实现的抽取java文件的注释,也可以用正则来实现,正则写法以后更新,
0 0
原创粉丝点击