/**
*
*/
package test;
/**
*字符替换
*/
public class ReplaceAll {
public ReplaceAll()
{
}
public static String replace(String s, String s1, String s2)
{
if(s == null)
return null;
StringBuffer stringbuffer = new StringBuffer();
int i = s.length();
int j = s1.length();
int k;
int l;
for(k = 0; (l = s.indexOf(s1, k)) >= 0; k = l + j)
{
stringbuffer.append(s.substring(k, l));
stringbuffer.append(s2);
}
if(k < i)
stringbuffer.append(s.substring(k));
return stringbuffer.toString();
}
}
——————————————————————————————————
import javax.servlet.http.*;
import java.util.Calendar;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Calendar calendar=Calendar.getInstance();
String fileName=String.valueOf(calendar.getTimeInMillis())+".html";
String tempFile=request.getRealPath("/template")+"//profession.template";
try {
String tempContent="";
FileInputStream fileinputstream=new FileInputStream(tempFile);
int length=fileinputstream.available();
byte bytes[]=new byte[length];
fileinputstream.read(bytes);
fileinputstream.close();
tempContent=new String(bytes);
//tempContent.replaceAll("//<%title%>",pform.getPname());
tempContent=ReplaceAll.replace(tempContent,"<$title$>",pform.getPname());
tempContent=ReplaceAll.replace(tempContent,"<$ptype$>",pform.getPtype());
tempContent=ReplaceAll.replace(tempContent,"<$place$>",pform.getPlace());
tempContent=ReplaceAll.replace(tempContent,"<$content$>",pform.getPdetail());
tempContent=ReplaceAll.replace(tempContent,"<$pamount$>",pform.getPamount());
fileName=request.getRealPath("/news/")+"/"+"profession//"+fileName;
FileOutputStream fileoutputstream=new FileOutputStream(fileName);
byte tag_bytes[]=tempContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
// WriteFile.writeInFile(pform.getPname(),pform.getPdetail(),tempFile,fileName,"profession//",request);
}
catch (Exception e) {
e.printStackTrace();
}