服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

一个最简单的 javabeanmaker(原创)


  只实现最简单功能,自动生成setter,getter当bean中属性较多时可以节省时间:

usage: java javabeanmaker aaa.txt bbb

1. aaa.txt is the text file in following style,you can get a example--jeru.txt in attachment
   ======================
   int id
   string name
   int age
   ======================

2  bbb is the file name of your javabean without .java,so if you want a test.java
   just type "java javabeanmaker aaa.txt test"

================= jeru.txt ==========================
int id
string name
int age
================= javabeanmaker.java ================
import java.io.*;
import java.util.*;

public class javabeanmaker {
  
  public static void main(string[] args) {
    system.out.println("reading datas......");
      try {
        // read properties of source text file
    int i = 0;
    string record = new string();
    vector property = new vector();
        randomaccessfile source = new randomaccessfile(args[0],"r");
        while ((record = source.readline()) != null) {
          i ++;
          property.addelement(record);        
        }
        source.close();
      
    randomaccessfile destine = new randomaccessfile(args[1],"rw");
        string content = "// this javabean is make by jeru's javabeanmaker" + "/r/n/r/n";
        content += "public class " + args[1] + " {" + "/r/n/r/n";
      
    string[] tmp = new string[3];
    for (i=0; i<property.size(); i++) {
      string str = (string)property.elementat(i);
      //system.out.println("value " + i + ":" + str);
      stringtokenizer tokens = new stringtokenizer(str);
          for (int j=0; j<=1; j++) {
        tmp[j] = tokens.nexttoken();
        system.out.println("token: " + tmp[j]);          
      }

         // capital tmp[1]
      tmp[2] = tmp[1].substring(0,1).touppercase() + tmp[1].substring(1);
      system.out.println(tmp[2]);
    
      content += "  " + tmp[0] + " " + tmp[1] + ";" + "/r/n";
        
      content += "  " + "public void set" + tmp[2] + "(" + tmp[1] + ") {" + "/r/n";
      content += "    " + "this." + tmp[1] + " = " + tmp[1] + ";" + "/r/n";
      content += "  " + "}" + "/r/n";
        
      content += "  " + "public "  + tmp[0] + " get" + tmp[2] + "() {" + "/r/n";
      content += "    " + "return " + tmp[1] + ";" + "/r/n";
          content += "  " + "}" + "/r/n/r/n";

       }

       system.out.println("the last content is " + content);
       content += "}";
       destine.writebytes(content);
       destine.close();    
    
     } catch(exception e){ e.getmessage(); }
  }
}

扫描关注微信公众号