平时做一个项目都要建很多的属性类,方法类和View类,学了IO流之后想了一种办法,就是自己写一串代码,然后在文本文档里写class <类名>加上所有的属性,然后运行一下代码就自动生成了一个 <类名>.java 文件。用myeclipse打开就可以了。同样的道理,你写一个只有 class <类名> 的txt文本文档
点一下另一段代码就自动生成了一个方法类,里面带有增删改查四个简单方法的方法类,还有一个View类。
首先说一下,生成属性类的这段代码:
package com.jereh.Generate; import java.io.*; import java.util.*; public class Generate1 { /** * @param args */ public static void main(String[] args) { FileReader fr = null; BufferedReader br = null; FileWriter fw = null; BufferedWriter bw = null; int i=0; File file1 = new File("E:\\IO\\原始\\1.txt"); File file2 = null; try { StringBuffer sbff = new StringBuffer(); fr = new FileReader(file1); br = new BufferedReader(fr); String next; while(!((next = br.readLine())==null)){ if(i==0){ sbff.append("public "); sbff.append(next); sbff.append("{\r\n"); String[] a = next.split(" "); file2 = new File("E:\\IO\\生成\\"+a[1]+".java"); }else{ sbff.append(" private "); sbff.append(next+"\r\n"); } i++; } sbff.append("}"); fw = new FileWriter(file2); bw = new BufferedWriter(fw); bw.write(sbff.toString()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
然后是生成方法类的代码:
<span style="font-size:18px;">package com.jereh.Generate; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Generate2 { public static void main(String[] args){ FileReader fr = null; BufferedReader br = null; FileWriter fw = null; BufferedWriter bw = null; StringBuffer sbff = new StringBuffer(); int i=0; File file1 = new File("E:\\IO\\原始\\2.txt"); File file2 = null; try { fr = new FileReader(file1); br = new BufferedReader(fr); String next; while(!((next = br.readLine())==null)){ if(i==0){ String[] a = next.split(" "); file2 = new File("E:\\IO\\生成\\"+a[1]+".java"); sbff.append("public "); sbff.append(next); sbff.append("{\r\n"); } i++; } sbff.append("\t public void add(){}\r\n"); sbff.append("\t public void delete(){}\r\n"); sbff.append("\t public void update(){}\r\n"); sbff.append("\t public void search(){}\r\n"); sbff.append("}"); fw = new FileWriter(file2); bw = new BufferedWriter(fw); bw.write(sbff.toString()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }</span>
最后是生成View界面的代码:
<span style="font-size:18px;">package com.jereh.Generate; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Generate3 { public static void main(String[] args){ FileReader fr = null; BufferedReader br = null; FileWriter fw = null; BufferedWriter bw = null; StringBuffer sbff = new StringBuffer(); int i=0; File file1 = new File("E:\\IO\\原始\\3.txt"); File file2 = null; try { fr = new FileReader(file1); br = new BufferedReader(fr); String next; while(!((next = br.readLine())==null)){ if(i==0){ String[] a = next.split(" "); file2 = new File("E:\\IO\\生成\\"+a[1]+".java"); sbff.append("public "); sbff.append(next); sbff.append("{\r\n"); } i++; } sbff.append("\t public void addView(){}\r\n"); sbff.append("\t public void deleteView(){}\r\n"); sbff.append("\t public void updateView(){}\r\n"); sbff.append("\t public void searchView(){}\r\n"); sbff.append("\t public void main(){\r\n"); sbff.append("\t \t System.out.println();\r\n"); sbff.append("\t \t System.out.println();\r\n"); sbff.append("\t \t System.out.println();\r\n"); sbff.append("\t \t System.out.println();\r\n"); sbff.append("\t \t System.out.println();\r\n"); sbff.append("\t \t int i = input.nextInt();\r\n"); sbff.append("\t \t switch(i){\r\n"); sbff.append("\t \t case 1:\r\n"); sbff.append("\t \t addView();\r\n"); sbff.append("\t \t break;\r\n"); sbff.append("\t \t case 2:\r\n"); sbff.append("\t \t deleteView();{\r\n"); sbff.append("\t \t break;\r\n"); sbff.append("\t \t case 3:\r\n"); sbff.append("\t \t updateView();{\r\n"); sbff.append("\t \t break;\r\n"); sbff.append("\t \t case 4:\r\n"); sbff.append("\t \t searchView();{\r\n"); sbff.append("\t \t break;\r\n"); sbff.append("\t \t default:\r\n"); sbff.append("\t \t break;{\r\n"); sbff.append("\t \t}\r\n"); sbff.append("\t }\r\n"); sbff.append("}"); fw = new FileWriter(file2); bw = new BufferedWriter(fw); bw.write(sbff.toString()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } </span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-25 05:42:43