Java实现的代码生成器

package test;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
/**
 * 代码生成器
 * @author penghuaiyi
 * @date 2011-09-09
 */
public class CodeUtil {

	public static void createHtml(){
		OutputStream os = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;
		try{
			os = new FileOutputStream("c:\\temp\\test.html");
			osw = new OutputStreamWriter(os,"UTF-8");
			bw = new BufferedWriter(osw);
			bw.write("<html>\r\n");
			bw.write("<head>\r\n");
			bw.write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
			bw.write("<title>文本测试</title>\r\n");
			createScript(bw);
			bw.write("</head>\r\n");
			bw.write("<body οnlοad=\"init()\">\r\n");
			createTable(bw);
			bw.write("</body>\r\n");
			bw.write("</html>\r\n");
			bw.flush();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				osw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	public static void createScript(BufferedWriter bw) throws IOException{
		bw.write("<script type=\"text/javascript\">\r\n");
		bw.write("\tfunction init(){\r\n");
		bw.write("\t\talert('初始化');\r\n");
		bw.write("\t}\r\n");
		bw.write("</script>\r\n");
	}
	
	public static void createTable(BufferedWriter bw) throws IOException{
		bw.write("<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\r\n");
		bw.write("<tr>\r\n");
		for(int j=0;j<5;j++){
			bw.write("<td>列"+j+"</td>\r\n");
		}
		bw.write("</tr>\r\n");
		for(int i=0;i<10;i++){
			bw.write("<tr>\r\n");
			for(int j=0;j<5;j++){
				bw.write("<td>"+j+"</td>\r\n");
			}
			bw.write("</tr>\r\n");
		}
	    bw.write("</table>\r\n");
	}
	
	public static void main(String[] args){
         createHtml();
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值