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");
}
}
}