Java JSON code is automatically generated

Source: Internet
Author: User
Tags readfile

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<string> ReadFile (String filename)
{
list<string> fileline = new arraylist<string> ();

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> line)
{
String cr= "\ r \ n";

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<string> fileline = ReadFile ("Field.txt");

Genaratecode (Fileline);
System.out.println ("success!");

}

}



Field.txt

Name,string
Url,string
Duration,string
Desc,string

Java JSON code is automatically generated

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.