import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
@SuppressWarnings("unchecked")
public class CodeGenerationTool {
private static final String DAO_PATH = "test/";
private static final String DAO_URL = "test";
String beanName = "demo";
// 生成dao
public static void createBeanDao(Class c) {
String cName = c.getName();
File file=new File(System.getProperty("user.dir") + "/src/"+DAO_PATH);
if(!file.exists()){
file.mkdir();
}
String fileName = System.getProperty("user.dir") + "/src/"+DAO_PATH+ cName + "Dao.java";
File f = new File(fileName);
try {
FileWriter fw = new FileWriter(f);
fw.write("package "+DAO_URL+";"+"public class " + cName
+ "Dao {}");
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CodeGenerationTool.createBeanDao(demo02.class);
}还可以深入的封装和扩展
}
来源:oschina
链接:https://my.oschina.net/u/2850633/blog/789771