Android automatically creates a model or PO file

Source: Internet
Author: User

In the development process found that the model or PO layer data is only different parameters, the others are the same, and each time the writing of the same thing will take a lot of time to write or copy. Although some can be generated quickly, but still need time, such as the Parcelable interface implementation of the need to write something, now use a file to write a class to automatically generate files believe that can reduce more time. Even if the development of the interface enough specification, can also directly parse the interface to generate model or PO files.


The source code is as follows:

Import Java.io.file;import java.io.fileoutputstream;import java.io.randomaccessfile;import java.util.Date;/** * Generate model or PO file at specified location * @author xiaoyi * * March 6, 2015 */public class Createmode {/** * Determine and create files and folders at specified location * * @param dirpath * @param fileName * @return * @throws Exception */public static void CreateFile (String dirpath, String fileName) throws Excep tion {try {file Dir = new File (Dirpath); File File = new file (dir, fileName);//Determine and create a folder at the specified location if (dir.exists ()) {System.out.println ("Model folder exists");} else { SYSTEM.OUT.PRINTLN ("Model folder does not exist, create model");d ir.mkdir ();//does not exist create}//to determine and create file if (File.exists ()) in the specified location { SYSTEM.OUT.PRINTLN (filename + "file exists");} else {System.out.println (filename + "file does not exist, new" + filename "); File.createnewfile ();//does not exist create}} catch (Exception e) { SYSTEM.OUT.PRINTLN (filename + "File creation failed! "); E.printstacktrace ();}}            /** * Writes the specified content to the specified file * * @param content * Write contents * @param filename * file name * @param bytetype * Write Format gbk,utf-8 ... * @return * @throws Exception */pubLic static Boolean writetxtfile (StringBuilder content, File filename,string bytetype) throws Exception {Randomaccessfile MM = Null;boolean flag = false; FileOutputStream o = null;try {o = new FileOutputStream (fileName); O.write (Content.tostring (). GetBytes (Bytetype)); O. Close (); flag = true;} catch (Exception e) {System.out.println ("File content Write failed! "); E.printstacktrace ();} finally {if (mm! = null) {Mm.close ();}} return flag;} /** * Create content to write * * @param packagename * Project package name such as: COM.XY.POCKET_BOOK.PO * @param remark * Notes to the PO            * @param author * author * @param time * created * @param otherinfo * Other instructions * @param className * Class name * @param ImpI * Interface required to implement Parcelable or Serializable * @param variable * member variable * @return */private static StringBuilder createcontant (string packagename,string remark, string author, string time, String Otherin Fo,string className, String ImpI, string ... variable) {StringBuilder builder = new StringBuilder ();//+ Project package path Builder.append ("packages"). Append (PackageName). Append ("; \n\r\n\r");/+ reference Package Builder.append ("Import Android.os.parcel;\n\r "). Append (" Import android.os.parcelable;\n\r\n\r ");//+po Comment Author date builder.append ("/**\n\r "). Append ("*"). Append (Remark). Append ("\n\r"), Builder.append ("*"). Append ("\n\r"); Builder.append ("*"). Append ("@ Author "). Append (author). Append (" \ n "), Builder.append (" * "). Append (" \n\r "); Builder.append (" * "). Append (Time). Append ("\n\r"); Builder.append ("*"). Append ("\n\r"); Builder.append ("*"). Append (Otherinfo). Append ("\n\r"); Builder.append ("*"). Append ("\n\r"), Builder.append ("* *"). Append ("\n\r");//+ Class name Interface Builder.append ("public class"). Append (ClassName). Append ("Implements"). Append (ImpI). Append ("{\n\r");//+ variable for (int i = 0; i < variable.length; i++) {builder.append ("\tprivate String"). Append (Variable[i]). append ("; \ n");} Builder.append ("\n\r\n\r");//+ Empty construction builder.append ("\tpublic"). Append (ClassName). Append ("() {}\n\r\n\r");//+ Parameter construction builder.append ("\tpublic"). Append (ClassName). Append ("("); for (int i = 0; i < variable.length; i++) {if (i = = variable.length-1) {builder.append ("String"). Append (Variable[i]);} else {builder.append ("String"). Append (Variable[i]). Append (",");}} Builder.append (") {\n\r"), Builder.append ("\t\tsuper (); \ n"); for (int i = 0; i < variable.length; i++) {Builder.append ( "\t\tthis."). Append (Variable[i]). Append ("="). Append (Variable[i]). append ("; \ n");} Builder.append ("\t}\n\r\n\r");//+tostringbuilder.append ("\[email protected]\n"); Builder.append ("\tpublic String toString () {\ n '); Builder.append ("\t\treturn \" "). Append (ClassName). Append (" ["); for (int i = 0; i < variable.length; i++) {if (i = = variable.length-1) {//name= "+ name +" Builder.append (Variable[i]). Append ("=\" + "). Append (Variable[i]). Append ("+ \" ");}  else if (i = = 0) {//name= "+ name +", Builder.append (Variable[i]). Append ("=\" + "). Append (Variable[i]). Append (" + \ ",");} else {builder.append ("). Append (Variable[i]). Append (" =\ "+"). Append (variAble[i]). Append ("+ \", ");}} Builder.append ("]\"; \ n "); Builder.append (" \t}\n\r\n\r ");//+getfor (int i = 0; i < variable.length; i++) {String str1 = Variable[i].substring (0, 1); String str2 = variable[i].substring (1, Variable[i].length ()); Builder.append ("\tpublic String get"). Append ( Str1.touppercase ()). Append (str2). Append ("() {\ n"), Builder.append ("\t\treturn"). Append (Variable[i]). append ("; \ n"). ); Builder.append ("\t}\n\r");} +setfor (int i = 0; i < variable.length; i++) {String str1 = variable[i].substring (0, 1); String str2 = variable[i].substring (1, Variable[i].length ()); Builder.append ("\tpublic void Set"). Append ( Str1.touppercase ()). Append (str2). Append ("(String"). Append (Variable[i]). Append (") {\ n"); Builder.append ("\t\tthis ."). Append (Variable[i]). Append ("="). Append (Variable[i]). append ("; \ n"); Builder.append ("\t}\n\r");} If you use the Parcelable interface to add two methods one implementation if (Impi.equals ("parcelable")) {//The first method Builder.append ("\[email protected]\n"); Builder.append ("\tpublic int describecontents () {\ n"); Builder.append ("\t\treturn 0;\n"); Builder.append ("\t}\n\r");//The second method Builder.append ("\[email protected]\n") ; Builder.append ("\tpublic void Writetoparcel (Parcel dest, int flags) {\ n"); for (int i = 0; i < variable.length; i++) {b Uilder.append ("\t\tdest.writestring ("). Append (Variable[i]). Append ("); \ n");} Builder.append ("\t}\n\r");//The third implementation of Builder.append ("\tprivate static Final parcelable.creator<"). Append (className ). Append ("> CREATOR = new creator<"). Append (ClassName). Append ("> () {\n\r"); Builder.append ("\t\[email  Protected]\n "), Builder.append (" \t\tpublic "). Append (ClassName). Append (" [] newarray (int size) {\ n "); Builder.append ( "\t\t\treturn new"). Append (className) append ("[size];\n"); Builder.append ("\t\t}\n\r"); Builder.append ("\t\[email  protected]\n "), Builder.append (" \t\tpublic "). Append (ClassName). Append (" Createfromparcel (Parcel source) {\ n " ), Builder.append ("\t\t\t"). Append (ClassName). Append ("Msource = new"). Append (ClassName). Append ("(); \ n"); for (int i = 0; I < Variable.length; i++) {builder.append ("\t\t\tmsource."). Append (Variable[i]). Append ("= source.readstring (); \ n");} Builder.append ("\t\t\treturn msource;\n"); Builder.append ("\t\t}\t\n"); Builder.append ("\t};\n\r"); builder.append ("};");} return builder;} public static void Main (string[] args) {//Line break/n/r/t//StringBuilder builder = new StringBuilder;//Builder.append (" 621363 "). Append (" 1 ")//1. Create a file try {createFile (" C:\\users\\chengxuyuan2\\desktop\\model "," Aa.java ");} catch ( Exception e) {e.printstacktrace ();} 2. Create write content StringBuilder builder = createcontant ("Com.xy.pocket_book.po", "Test Notes", "Xiaoyi", New Date (). toLocaleString ( ), "Other information notes", "Billpo", "parcelable", "id", "title", "parent_id", "remark");//3. Write the contents of file F = new file ("C:\\users\\chengxuy Uan2\\desktop\\model\\aa.java "); try {writetxtfile (builder, F," GBK "); System.out.println ("File Write succeeded! ");} catch (Exception e) {System.out.println ("File write Failed!") "); E.printstacktrace ();}}}


Android automatically creates a model or PO file

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.