隨筆 - 8  文章 - 24  trackbacks - 0
          <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(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 閱讀(789) 評論(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。 等可以提高你的編程效率。  回復  更多評論
            

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


          網站導航:
           
          主站蜘蛛池模板: 大余县| 巴东县| 安龙县| 琼中| 高陵县| 浠水县| 琼结县| 沈丘县| 双峰县| 湛江市| 会理县| 咸丰县| 瑞金市| 新泰市| 孝昌县| 樟树市| 平遥县| 会宁县| 宁城县| 古浪县| 荥阳市| 温州市| 温泉县| 平邑县| 东乡| 田阳县| 南昌县| 宾川县| 和平县| 大兴区| 遂昌县| 德令哈市| 彭山县| 策勒县| 高邑县| 抚远县| 七台河市| 东阳市| 调兵山市| 龙泉市| 鄂托克前旗|