隨筆 - 8  文章 - 24  trackbacks - 0
          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(4)

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          package com.mypcs.utils;

          import java.io.*;
          import java.util.*;

          /**
           * 生成Html文件的工具類
           * alex 2007-5-29 下午09:54:14
           
          */

          public class CreateHtml {
              
              
          public CreateHtml(){
                  
              }

              
              
          public String readHtml(String fileName){
                  StringBuffer sb 
          = new StringBuffer();
                  
                  
          try{
                      fileName 
          = fileName.replaceAll("\\\\""/");
                      FileInputStream fis 
          = new FileInputStream(fileName);
                      BufferedReader reader 
          = new BufferedReader(new InputStreamReader(fis));
                  
                      String line 
          =reader.readLine();
                      
          while(line != null){
                          sb.append(line);
                          sb.append(
          "\r\n");
                          line 
          = reader.readLine();
                      }

                      reader.close();
                      fis.close();
                  }
          catch(Exception ex){
                      
          return "";
                  }

                  
                  
          return sb.toString();
              }

              
              
          public void writeHtml(String nowPath,String fileName,String s){
                  
          try{
                      
          //分級建目錄
                      String needBulid = fileName.substring(0, fileName.lastIndexOf("\\"));
                      needBulid 
          = needBulid.substring(nowPath.length());
                      String array[] 
          = needBulid.split("\\\\");
                      nowPath 
          = nowPath.replaceAll("\\\\""/");
                      String bulidNow 
          = nowPath.substring(0,nowPath.length()-1);
                      
          for (int i = 0; i < array.length; i++{
                          String temp 
          = array[i];
                          
          if(temp!=null&&temp.length()>0){
                              bulidNow 
          = bulidNow + "/" + temp;
                              File file 
          = new File(bulidNow);
                              
          while(!file.exists()){
                                  file.mkdirs();
                              }

                          }

                      }

                      
                      fileName 
          = fileName.replaceAll("\\\\""/");
                      File outFile  
          = new File(fileName);
                      
          while(!outFile.exists()){
                          outFile.createNewFile();
                      }

                      FileWriter writer 
          = new FileWriter(new File(fileName));
                      BufferedWriter bufferedWriter 
          = new BufferedWriter(writer);
                      bufferedWriter.write(s);
                      bufferedWriter.close();
                      writer.close();
                      
                  }
          catch(Exception ex){
                      ex.printStackTrace();
                  }

              }

              
              
          public String setValue(String s,List list,HashMap hashMap){
                  
          for(int i = 0; i < list.size(); i++){
                      String name 
          = (String)list.get(i);
                      
          if(name==null){
                          name 
          = "";
                      }

                      String value 
          = (String)hashMap.get(name);
                      
          if(value==null){
                          value 
          = "";
                      }

                      value 
          = value.replaceAll("\\$""#美元#");
                      s 
          = s.replaceAll("#"+name+"#",value);
                  }

                  
          return s;
              }

          }


          沒有亂碼問題,呵呵,希望能幫到一些朋友。
          posted on 2007-07-16 15:34 Vincent.Yu 閱讀(780) 評論(2)  編輯  收藏

          FeedBack:
          # re: 貼一個生成靜態HTML用的工具類 2007-09-10 14:58 久城
          問一個比較低級的問題,網站中的很多動態增加的HTML頁面就是這樣作成的嗎?
          比如csdn上每天的一些動態新聞的網頁,都是一個HTML頁面,目錄結構可能是www.csdn.net/news/20070910/0001.html,這樣的頁面都是如何生成的?是不是也是用一個專門生成HTML頁面的類來自動生成啊?
          迷惑中,還請指教。  回復  更多評論
            
          # re: 貼一個生成靜態HTML用的工具類 2007-09-15 20:40 Alexander.Yu
          也不全是這樣生成的,這個是用于一些簡單的生成。復雜點的要用模版引擎,如Freemarker,Apache Velocity。 等可以提高你的編程效率。  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 伊宁县| 徐州市| 荥经县| 疏勒县| 新昌县| 伊宁市| 革吉县| 松江区| 宁国市| 杨浦区| 光泽县| 呼和浩特市| 浦江县| 方山县| 长顺县| 四川省| 南靖县| 喀什市| 夏津县| 阿勒泰市| 白银市| 翁源县| 秦安县| 宁海县| 南澳县| 阿合奇县| 长沙县| 桂平市| 双城市| 寻乌县| 民县| 宁陵县| 华坪县| 镇沅| 四子王旗| 右玉县| 安达市| 青海省| 凤翔县| 麻城市| 鸡泽县|