读取指定类的注释
想要爬取ActionEnter.java的注释:
package com.baidu.ueditor;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.RootDoc;
/**
* 读取指定类的注释
* @Desc
*/
public class JavaDocUse {
private static RootDoc rootDoc;
public static class Doclet {
public static boolean start(RootDoc rootDoc) {
JavaDocUse.rootDoc = rootDoc;
return true;
}
}
public static void show() {
ClassDoc[] classes = rootDoc.classes();
for (ClassDoc classDoc : classes) {
System.out.println(classDoc.name() + "类的注释:" + classDoc.commentText());
}
}
public static void main(String[] args) {
String property = System.getProperty("user.dir");
com.sun.tools.javadoc.Main.execute(new String[]{"-doclet",
Doclet.class.getName(),
"-encoding", "utf-8", "-classpath",
// 不大清楚作用,默认为本工具类存放地址
property + "\\gp6-system\\src\\main\\java\\com\\baidu\\ueditor"
// 指定要提取注释的文件夹/类
, property + "\\gp6-system\\src\\main\\java\\com\\baidu\\ueditor\\ActionEnter.java"
});
show();
}
}
结果:
控制台打印出项目的包名和类名,再打印出注释
我们知道Java提供api生成Javadoc。那也可以通过Javadoc提取出项目注释:
package com.baidu.ueditor;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.RootDoc;
import java.io.File;
/**
* 打印出包名和类名,再打印出注释
*/
public class TreeUtil {
private static RootDoc rootDoc;
public static class Doclet {
public static boolean start(RootDoc rootDoc) {
TreeUtil.rootDoc = rootDoc;
return true;
}
}
public static void main(String[] args) throws Exception {
String packageName = "";
// 获取当前项目地址
File root = new File(System.getProperty("user.dir") + "\\");
loop(root, packageName);
}
public static void loop(File folder, String packageName) throws Exception {
// 加载当前项目所有文件
File[] files = folder.listFiles();
for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
File file = files[fileIndex];
if (file.isDirectory()) {
// 当是文件夹的时候接着遍历
loop(file, packageName + file.getName() + "/");
} else {
// 文件时,进行注释提取
listMethodNames(file.getName(), packageName);
}
}
}
public static void listMethodNames(String filename, String packageName) {
try {
// 提取后缀
String suffix = filename.substring(filename.length() - 5, filename.length());
// 当是Java文件时,查询它的注释
if (suffix.equals(".java")) {
String property = System.getProperty("user.dir");
com.sun.tools.javadoc.Main.execute(new String[]{"-doclet",
TreeUtil.Doclet.class.getName(),
"-encoding", "utf-8", "-classpath",
property + "\\gp6-system\\src\\main\\java\\com\\baidu\\ueditor"
, packageName + filename
});
// 拿到类注释
ClassDoc[] classes = rootDoc.classes();
for (ClassDoc classDoc : classes) {
System.out.println(packageName + filename + "-------------------------"+ classDoc.commentText());
}
}
} catch (Exception e) {
System.out.println("exception = " + e.getLocalizedMessage());
}
}
}
结果:
txt输出 项目的类名,对应注释
因为上面在控制台会打印很多错误,影响我们获取注释,可以写入到txt文件中:
package com.baidu.ueditor;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.RootDoc;
import java.io.File;
/**
* 打印出包名和类名,再打印出注释
*/
public class TreeUtil {
private static RootDoc rootDoc;
public static class Doclet {
public static boolean start(RootDoc rootDoc) {
TreeUtil.rootDoc = rootDoc;
return true;
}
}
public static void main(String[] args) throws Exception {
String packageName = "";
// 获取当前项目地址
File root = new File(System.getProperty("user.dir") + "\\");
loop(root, packageName);
}
public static void loop(File folder, String packageName) throws Exception {
// 加载当前项目所有文件
File[] files = folder.listFiles();
for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
File file = files[fileIndex];
if (file.isDirectory()) {
// 当是文件夹的时候接着遍历
loop(file, packageName + file.getName() + "/");
} else {
// 文件时,进行注释提取
listMethodNames(file.getName(), packageName);
}
}
}
public static void listMethodNames(String filename, String packageName) {
try {
// 提取后缀
String suffix = filename.substring(filename.length() - 5, filename.length());
// 当是Java文件时,查询它的注释
if (suffix.equals(".java")) {
String property = System.getProperty("user.dir");
com.sun.tools.javadoc.Main.execute(new String[]{"-doclet",
TreeUtil.Doclet.class.getName(),
"-encoding", "utf-8", "-classpath",
property + "\\gp6-system\\src\\main\\java\\com\\baidu\\ueditor"
, packageName + filename
});
// 拿到类注释
ClassDoc[] classes = rootDoc.classes();
for (ClassDoc classDoc : classes) {
System.out.println(filename + "-------------------------" + classDoc.commentText());
txtExport.creatTxtFile("ces");
txtExport.writeTxtFile(filename + "-------------------------" + classDoc.commentText());
}
}
} catch (Exception e) {
System.out.println("exception = " + e.getLocalizedMessage());
}
}
}
package com.baidu.ueditor;
import java.io.*;
public class txtExport {
private static String path = "D:/";
private static String filenameTemp;
/**
* 创建文件
* @throws IOException
*/
public static boolean creatTxtFile(String name) throws IOException {
boolean flag = false;
filenameTemp = path + name + ".txt";
File filename = new File(filenameTemp);
if (!filename.exists()) {
filename.createNewFile();
flag = true;
}
return flag;
}
/**
* 写文件
* @param newStr 新内容
* @throws IOException
*/
public static boolean writeTxtFile(String newStr) throws IOException {
// 先读取原有文件内容,然后进行写入操作
boolean flag = false;
String filein = newStr + "\r\n";
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
// 文件路径
File file = new File(filenameTemp);
// 将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该文件原有的内容
for (int j = 1; (temp = br.readLine()) != null; j++) {
buf = buf.append(temp);
// 行与行之间的分隔符 相当于“\n”
buf = buf.append(System.getProperty("line.separator"));
}
buf.append(filein);
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
flag = true;
} catch (IOException e1) {
// TODO 自动生成 catch 块
throw e1;
} finally {
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return flag;
}
}
结果: