也許下面的代碼可以幫到你。
它將打印每個文件夾及其創建的每個文件。如果該文件是*.java
,則代碼將輸入該文件並打印與java方法匹配的所有內容。
另外,如果需要,更改REGEX,確定什麼是方法。該生產線是:
public static final String JAVA_METHOD_REGEX = "(public|protected|private|static|\\s) +[\\w\\<\\>\\[\\]]+\\s+(\\w+) *\\([^\\)]*\\)";
全碼:
public class ReadFilesAndMethods {
public static void main(String[] args) throws Exception {
File folder = new File("C:/Workspace/MyProject/src");
listFiles(folder, "");
}
public static final String JAVA_FILE_REGEX = "^\\w+\\.java";
public static final String JAVA_METHOD_REGEX = "(public|protected|private|static|\\s) +[\\w\\<\\>\\[\\]]+\\s+(\\w+) *\\([^\\)]*\\)";
public static void listFiles(File folder, String prefix) throws Exception {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
System.out.println(prefix + fileEntry.getName() + "/");
listFiles(fileEntry, prefix + "\t");
} else {
System.out.println(prefix + fileEntry.getName());
if (fileEntry.getName().matches(JAVA_FILE_REGEX)) {
readMethodsFromJavaFile(fileEntry.getAbsolutePath(), prefix + "\t");
}
}
}
}
public static void readMethodsFromJavaFile(String fileName, String prefix) throws Exception {
Scanner scanner = new Scanner(new File(fileName));
String text = scanner.useDelimiter("\\A").next();
scanner.close();
Pattern pattern = Pattern.compile(JAVA_METHOD_REGEX);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(prefix+matcher.group(0));
}
}
}
輸出:
main/
java/
utils/
UsefulStuff.java
public Long getSomething(Class<?> classe)
public Whatever load(String anything)
Start.java
static void main(String[] args)
entity/
system/
User.java
public long getId()
public void setId(long id)
...
看一看ant4eclipse。 – 2013-05-08 06:05:56
@ThorbjørnRavnAndersen - 會看起來! :) – ComputerFellow 2013-05-08 06:07:58
也許javadoc可以幫助你。 – Sammy 2013-05-08 06:14:15