JAVA生成文件

package com.zph.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

/**
 * @author pinghui.zhang
 *
 */
public class StringToFile {
 
    /**
    * 把字符串写入文本中
    * @param fileName 生成的文件绝对路径
    * @param content 文件要保存的内容
    * @param enc  文件编码
    * @return
    */
    public static boolean writeStringToFile(String fileName,String content,String enc) {
        File file = new File(fileName);
        
        try {
            if(file.isFile()){
                file.deleteOnExit();
                file = new File(file.getAbsolutePath());
            }
            OutputStreamWriter os = null;
            if(enc==null||enc.length()==0){
                os = new OutputStreamWriter(new FileOutputStream(file));
            }else{
                os = new OutputStreamWriter(new FileOutputStream(file),enc);
            }
            os.write(content);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
 
    public static void main(String args[]){
        int[] ids = {995, 996, 997};
        StringBuffer presb = new StringBuffer();
        StringBuffer sufsb = new StringBuffer();
        presb.append("<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%>\n")
                .append("<%@ taglib uri=\"http://java.sun.com/jstl/core_rt\" prefix=\"c\"%>\n")
                .append("<%\n")
                .append("\tString deptid = \"");
  
        sufsb.append("\"; //只要修改此处即可\n")
                .append("\trequest.setAttribute(\"dept\", com.dadi.oa.system.DepartmentUtil.getDepartmentInfo(new Long(deptid)));\n")
                .append("\tString fromurl = request.getRequestURI().substring(request.getRequestURI().lastIndexOf(\"/\") + 1);\n")
                .append("%>\n")
                .append("<%@ include file=\"body.jsp\"%>");
  
        String content = "";
        String fileName = "";
  
        for(int i = 0; i < ids.length; i++){
            fileName = "D:\\project1\\OA\\Code\\jsp\\anth\\" + ids[i] + ".jsp";
            content = presb.toString() + ids[i] + sufsb.toString();
            writeStringToFile(fileName, content, "UTF-8");
        }
    }

}

posted on 2009-09-18 23:42 张平辉 阅读(159) 评论(0)  编辑  收藏 所属分类: JAVA


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年6月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

留言簿

文章分类

文章档案

搜索

最新评论