javajson代码自动生成(新建类)-创新互联

package code;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

public class Code {

   private static List readFile(String filename)
   {
      List fileline = new ArrayList();

      try {

         FileReader in = new FileReader(filename);
         LineNumberReader reader = new LineNumberReader(in);

         String s = reader.readLine();
         while( s != null )
         {
            if(!s.startsWith("#"))
            {
               fileline.add(s);
            }
            s = reader.readLine();
         }

         reader.close();
         in.close();
      }catch(Exception e)
      {
         e.printStackTrace();
      }

      return fileline;
   }

   public static void genarateCode(List line)
   {
      String cr=" ";

      try{

         String [] data = line.get(1).split(",");
         FileWriter fw = new FileWriter(data[1]+".java");
         fw.write(cr);
         data = line.get(0).split(",");
         fw.write("package "+data[1]+cr);
         fw.write(cr);
         fw.write(cr);
         fw.write("import com.google.gson.annotations.SerializedName;"+cr);
         fw.write(cr);
         fw.write(cr);
         data = line.get(1).split(",");
         fw.write("public class "+data[1]+" {"+cr);
         fw.write(cr);
           for(int i = 2;i < line.size();i++)
         {
            data = line.get(i).split(",");
            if(data[1].trim().equalsIgnoreCase("int"))
            {
              fw.write("@SerializedName(""+data[0]+"")"+cr);
              fw.write("private Integer "+toLowerCaseFirst(data[0])+";"+cr);
            }
            else if(data[1].trim().equalsIgnoreCase("string"))
            {
               fw.write("@SerializedName(""+data[0]+"")"+cr);
               fw.write("private String "+toLowerCaseFirst(data[0])+";"+cr);
            }
            else
            {
              fw.write("@SerializedName(""+data[0]+"")"+cr);
              fw.write("private "+ data[1]+" "+toLowerCaseFirst(data[0])+";"+cr);
            }
         }

           fw.write(cr);
           fw.write(cr);

           for(int i = 2;i < line.size();i++)
          {
              data = line.get(i).split(",");
             if(data[1].trim().equalsIgnoreCase("int"))
             {
                fw.write("public void set"+data[0]+"(Integer value)"+cr);
                fw.write("{"+cr);
                fw.write("   "+toLowerCaseFirst(data[0])+" = "+"value;"+cr);
                fw.write("}"+cr);

                fw.write("public Integer get"+data[0]+"()"+cr);
                fw.write("{"+cr);
                fw.write("   return "+toLowerCaseFirst(data[0])+";"+cr);
                fw.write("}"+cr);
             }
             else if(data[1].trim().equalsIgnoreCase("string"))
             {
                fw.write("public void set"+data[0]+"(String value)"+cr);
              fw.write("{"+cr);
              fw.write("   "+toLowerCaseFirst(data[0])+" = "+"value;"+cr);
              fw.write("}"+cr);

              fw.write("public String get"+data[0]+"()"+cr);
              fw.write("{"+cr);
              fw.write("   return "+toLowerCaseFirst(data[0])+";"+cr);
              fw.write("}"+cr);
             }
           else
           {
              fw.write("public void set"+data[0]+"("+data[1]+" value)"+cr);
            fw.write("{"+cr);
            fw.write("   "+toLowerCaseFirst(data[0])+" = "+"value;"+cr);
            fw.write("}"+cr);

            fw.write("public "+data[1]+" get"+data[0]+"()"+cr);
            fw.write("{"+cr);
            fw.write("   return "+toLowerCaseFirst(data[0])+";"+cr);
            fw.write("}"+cr);
           }
          }
           fw.write(cr+cr);
           fw.write("} "+cr);
           fw.close();
      }catch(Exception e)
      {

      }
   }

   public static String toLowerCaseFirst(String str)
   {
      StringBuilder sb = new StringBuilder(str);
      sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
      str = sb.toString();
      return str;
   }

   public static void main(String args[])
   {

      List fileline = readFile("field.txt");

        genarateCode(fileline);
        System.out.println("success!");

   }

}

成都创新互联是专业的丹棱网站建设公司,丹棱接单;提供成都网站制作、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行丹棱网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!field.txtjavajson代码自动生成(新
建类)

package,class
class,class
ContentList,List

version,int

name,String


文章题目:javajson代码自动生成(新建类)-创新互联
当前地址:http://abwzjs.com/article/cdpipd.html