从零开始学Java之IO字符流懒人模式(自动生成属性类,方法类,界面类)

   平时做一个项目都要建很多的属性类,方法类和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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值