欢迎光临
我们一直在努力

一个最简单的 JavaBeanMaker(原创)-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

只实现最简单功能,自动生成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 jerus 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(); }
  }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个最简单的 JavaBeanMaker(原创)-JSP教程,Java技巧及代码
分享到: 更多 (0)

相关推荐

  • 暂无文章