全部博文(178)
分类: Java
2008-04-15 18:04:04
import java.io.*;
import java.util.StringTokenizer;
class A {
}
public class Read extends A {
public static void phase(String fileName) {
String className = "";
String superClassName = "";
try {
FileReader fis = new FileReader(fileName);
BufferedReader reader = new BufferedReader(fis);
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("class")) {
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreElements()) {
String word = tokenizer.nextToken();
if (word.equals("class")) {
if (tokenizer.hasMoreElements()) {
className = tokenizer.nextToken();
}
} else if (word.equals("extends")) {
if (tokenizer.hasMoreElements()) {
superClassName = tokenizer.nextToken();
}
}
}
}
}
} catch (IOException ioex) {
ioex.printStackTrace();
}
System.out.println("ClassName: " + className
+ ", SuperClassName: "
+ superClassName);
}
/**
* @param args
*/
public static void main(String[] args) {
Read.phase("Read.java");
}
}
Pattern p = Pattern.compile("class\\s*(\\w+)(\\s+extends\\s+(\\w+)(\\s+.*?)?)?\\s*\\{",Pattern.DOTALL);
Matcher m = p.matcher("public class Test extends Object implements Runnable {");
if(m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(3));
}