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{
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
genarateCode(fileline);
System.out.println("success!");
}
}
package,class
class,class
ContentList,List
version,int
name,String
文章题目:javajson代码自动生成(新建类)-创新互联
当前地址:http://abwzjs.com/article/cdpipd.html