Java根据相应的静态页面模板,替换相应的内容

package com.haohaosh.common.util.web;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;

/**
 * 
 * @项目名 ssh
 * @功能 根据相应的静态页面模板,替换相应的内容;
 * @类名 TextToHtml
 * @作者 javalearns.com
 * @日期 Aug 30, 20113:44:55 PM
 * @版本 1.0
 */
@SuppressWarnings("unchecked")
public class TextToHtml {
 /**
  * 根据相应的模板替换相应的内容;
  * 
  * @param filePath
  *            模板地址;
  * @param htmlFile
  *            生成的静态页的路径;
  * @param replace
  *            替换的内容;Map<替换正则,替换后的内容>
  * @return
  */
 public static boolean TextToHtmlFile(String filePath, String htmlFile,
   Map<String, String> replace) {
  String str = "";
  try {
   String tempStr = "";
   FileInputStream is = new FileInputStream(filePath);
   BufferedReader br = new BufferedReader(new InputStreamReader(is));
   while ((tempStr = br.readLine()) != null)
    str = str + tempStr;
   is.close();
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }
  try {
   Iterator it = replace.entrySet().iterator();
   while (it.hasNext()) {
    Map.Entry context = (Map.Entry) it.next();
    String key = context.getKey().toString();
    String value = context.getValue().toString();
    str = str.replaceAll(key, value);
   }
   File f = new File(htmlFile);
   BufferedWriter o = new BufferedWriter(new FileWriter(f));
   o.write(str);
   o.close();
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }
  return true;
 }
}

转载 Java免费学习  Java自学网  http://www.javalearns.com

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值