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
{
List
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
{
String cr="
";
try{
FileWriter fw = new FileWriter("class.java");
for(int i = 0;i < line.size();i++)
{
String [] 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);
}
}
fw.write(cr);
fw.write(cr);
for(int i = 0;i < line.size();i++)
{
String [] 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);
}
}
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
genarateCode(fileline);
System.out.println("success!");
}
}
Name,String
Url,String
Duration,String
Desc,String
名称栏目:javajson代码自动生成-创新互联
文章源于:http://hbruida.cn/article/dgssji.html