我的代码生成器

Step 1 创建一个Main函数
None.gif import  java.io.BufferedReader;
None.gif
import  java.io.BufferedWriter;
None.gif
import  java.io.File;
None.gif
import  java.io.FileReader;
None.gif
import  java.io.FileWriter;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  TemplateApp  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) throws Exception dot.gif{
InBlock.gif        String outCode
="";
InBlock.gif        
InBlock.gif        String templateCode
=readFromTemplate("template.txt");
InBlock.gif        
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
        String[][] attr=dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{"item","Item"}
ExpandedSubBlockStart.gifContractedSubBlock.gif                ,
dot.gif{"case","Case"}
ExpandedSubBlockEnd.gif                }
;
InBlock.gif        
InBlock.gif        
for(int i=0,j=attr.length;i<j;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            String innerCode
=templateCode;
InBlock.gif            
for(int k=0,l=attr[i].length;k<l;k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                innerCode
=innerCode.replaceAll("#"+k+"#",attr[i][k]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            outCode 
+= innerCode + "\r\n\r\n";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
/
InBlock.gif
        
InBlock.gif        
InBlock.gif        writeToOut(
"out.txt",outCode);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private static String readFromTemplate(String file) throws Exception dot.gif{
InBlock.gif
InBlock.gif        String rCode 
= "";
InBlock.gif        File read 
= new File(file);
InBlock.gif        BufferedReader br 
= new BufferedReader(new FileReader(read));
InBlock.gif        String temp 
= null;
InBlock.gif        temp 
= br.readLine();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (temp != nulldot.gif{
InBlock.gif            rCode 
+= temp + "\r\n";
InBlock.gif            temp 
= br.readLine();
ExpandedSubBlockEnd.gif        }

InBlock.gif        br.close();
InBlock.gif
InBlock.gif        
return rCode;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private static void writeToOut(String file,String content) throws Exceptiondot.gif{
InBlock.gif        
InBlock.gif        File write 
= new File(file);
InBlock.gif        
InBlock.gif        BufferedWriter bw 
= new BufferedWriter(
InBlock.gif                 
new FileWriter(write));
InBlock.gif        
InBlock.gif        bw.write(content 
+ "\r\n");
InBlock.gif        
InBlock.gif        bw.flush();
InBlock.gif        
InBlock.gif        bw.close();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

Step 2:准备两个文件template.txt 和out.txt

Step 3:把有规律的代码放到template.txt中,把有规律的单词用#n#替换,修改main函数里的字符串数组

Step 4:运行

Step 5:到out.txt中拿代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值