随笔 - 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 阅读(751) 评论(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。 等可以提高你的编程效率。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: