您的位置:首页 > 其它

io流的一些特别用法

2016-06-21 21:17 218 查看
之前我们老大给我分配了个任务,要我按数据库表结构建下类,看了一下,尼玛37张表,字段多的有二三十个,想想也是蛋疼,不过还好之前我们老师有给我们说过有一种东西叫敏捷开发,那次给我们演示了下通过io流直接建出一个类,所以我果断的把代码翻出来加工了一下,主要的原理就是先建立模型,把格式固定的代码提取出来,把中间可能会变化的部分用一些特殊单词替换(只要不跟平时的会用到的属性名相同或者包含就行),建立成一个模板(也可以直接写在方法里),然后通过io去读这个模板,并通过字符串的替换、截取等操作把这个模板中的特殊单词替换掉。 以下是我按自己公司的数据库表结构写的自动建类

先是表设计的内容
//ep_info(企业信息表)
////企业信息表
//----------------------------------------------
//ep_id(企业编号) PKInteger(11)
//ep_name(企业名称) String(64)
//short_intro(企业简介) String(128)
//long_intro(企业详细介绍) String(2048)
//html_url(企业详情html地址) String(128)
//status(状态) Integer(4) //0=初始注册 1=正常 2=禁用 3=暂停 4=注销
//ep_logo(企业logo) String(128)
//contact_name(联系人) String(64)
//contact_phone(联系电话) String(128) //多个逗号分隔
//email(联系邮件) String(64) //多个逗号分隔
//address(联系地址) String(256)
//web_site(企业网站) String(128)
//ep_feature(企业特色说明) String(512)
//is_certification(是否实名认证) Integer(4) //0=否 1=是
//create_time(创建时间) Date
//modify_time(修改时间) Date
这个表设计包含了三部分,字段名、注解(每行的中文就当是注解)、类型,然后我们公司要求的类的写法是,多个单词组成的属性名,首字母小写,之后每个单的首字母大写,然后先是整理表设计的内容,我的做法是把三个部分分别截取掉不需要的部分,然后放入三个数组里,再按照一个类的格式把整个类打印出来,用io直接写成文件。


以下为全部代码,可以在简化一下的,不过懒的理了。
模板有两个,把上面的表设计的内容直接扔进model里面,model2是getset的模板(其实直接写方法里面就行了)

package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
/**
*
*
*
***/
public class test {
static String packname="order";//包名
static String beanName="bd_push";//类名

static String projectName="appFactory";//工程名
static String model2="d:model2.txt";//getset模板
static String basepath="E:\\myeclipse-workspace\\appFactory\\src\\com\\hzlq\\"+projectName+"\\"+packname+"\\dto";
static String packageName="com.hzlq.appFactory."+packname+".dto";//包名

public static void main(String[] args) throws IOException {
String model="d:model.txt";//参数模板
// TODO Auto-generated method stub
InputStream in =new FileInputStream(new File(model));
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String string="";
String attribute="";//属性
String beanType="";//类型
String notes="";//注释
StringBuffer StringBuffer=new StringBuffer("");
while(br.ready()){
string=(br.readLine().replaceAll(" ", ""));
if(string.indexOf("String")>=0){beanType="String";}
if(string.indexOf("Integer")>=0){beanType="Integer";}
if(string.indexOf("Date")>=0){beanType="Date";}
if(string.indexOf("Blob")>=0){beanType="Blob";}
if(string.indexOf("PKInteger")>=0){beanType="PKInteger";}
attribute=string.substring(0, string.indexOf("("));
int a=string.indexOf("(", string.indexOf(beanType));
int b=string.indexOf(")",string.indexOf(beanType));
String c="";
if(a>0&&b>0){
c=string.substring(a,b+1);
}
notes=string.replace(attribute, "").replace(beanType, "").replace(c, "");
StringBuffer.append(attribute+"*"+notes+"*"+beanType+"*");
}
System.out.println(StringBuffer.toString());
quick(StringBuffer.toString());

}
//str格式(属性名*注释*类型*)
public static void quick(String str) throws IOException{

while(beanName.indexOf("_")>=0){
int a=beanName.indexOf("_")+1;
String beanName2=beanName.substring(a,a+1);
beanName=beanName.replace("_"+beanName2,beanName2.toUpperCase());
}
File file=new File(basepath);
if(!file.exists()){
file.mkdir();
}
OutputStream out=new FileOutputStream(basepath+"\\"+beanName+".java");
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(out));
StringBuffer StringBuffer=new StringBuffer("");
str=str.replace("(","").replace(")", "").replace("//", "");
String array[]=str.split("[*]");
String attribute="";//属性
String parameter="";
String beanType="";//类型
bw.write("package "+packageName+";");
bw.newLine();
bw.write("public class "+beanName+" {");
bw.newLine();

//打印私有属性
for(int i=1;i<=array.length/3;i++){
String str2=array[3*i-3];//属性名
while(str2.indexOf("_")>=0){
int a=str2.indexOf("_")+1;
String str3=str2.substring(a,a+1);
str2=str2.replace("_"+str3,str3.toUpperCase());
}
String str4=array[3*i-1];//属性类型
if(str4.indexOf("String")>=0){str4="String";}
if(str4.indexOf("date")>=0){str4="Date";}
if(str4.indexOf("Integer")>=0){str4="Integer";}
if(str2.indexOf("id")>=0&&str4.indexOf("Integer")>=0){str4="Long";}
if(str2.indexOf("Id")>=0&&str4.indexOf("Integer")>=0){str4="Long";}
bw.write("private beanType beanname; //zhushi".replace("beanType",str4).replace("beanname",str2).replace("//zhushi", "//"+array[3*i-2]));
bw.newLine();
if(i<=array.length/3-1){parameter+=str4+" "+str2+",";}//拼接参数构造的参数
else{parameter+=str4+" "+str2;}
if(i<=array.length/3-1){attribute+=str2+",";}
else{attribute+=str2;}
if(i<=array.length/3-1){beanType+=str4+",";}
else{beanType+=str4;}
}

//打印无参构造
bw.write("public "+beanName+"(){");
bw.newLine();
bw.write(" super();");
bw.newLine();
bw.write("}");
bw.newLine();
//打印参数构造
String array2[]=attribute.split(",");
bw.write("public "+beanName+"("+parameter+"){");
bw.newLine();
bw.write(" super();");
bw.newLine();
for(int i=0;i<array2.length;i++){
bw.write(" this."+array2[i]+" = "+array2[i]+";");
bw.newLine();
}
bw.write("}");
bw.newLine();

//开始打印getset方法
String array3[]=beanType.split(",");
String Attribute="";
for(int i=0;i<array2.length;i++){
InputStream in2 =new FileInputStream(new File(model2));
BufferedReader br2=new BufferedReader(new InputStreamReader(in2));
Attribute=array2[i];
Attribute=Attribute.substring(0, 1).toUpperCase()+Attribute.substring(1);
while(br2.ready()){
bw.write(br2.readLine().replace("beanType", array3[i]).replace("attribute", array2[i]).replace("Attribute", Attribute));
bw.newLine();
}
}
bw.newLine();
bw.write("}");
bw.flush();
System.out.println("end");
}
}


很过代码结构差不多的地方都可以用这个方法,比如我们公司dao层用的是springJDBC,基本的增删改查格式基本上是一样的,我就把sql拼接的地方用这个方法直接打印在控制台上了,然后写sql的时候就直接写个头,剩下的直接复制就搞定了,要是在完善一下也可以直接把整个dao层生成出来,然后少数格式不同的地方改一下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  io流