实现抽取java中的注释的代码
2015-09-03 20:04
627 查看
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文件的注释,也可以用正则来实现,正则写法以后更新,
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统