码迷,mamicode.com
首页 > 编程语言 > 详细

java中使用String的replace方法替换html模板保存文件

时间:2016-05-07 16:38:51      阅读:845      评论:0      收藏:0      [点我收藏+]

标签:

在我们的D盘下有这样一个html模板,现在我们要做的就是解析news.template文件,从数据库中提取数据将数据添加到指定的模板位置上

技术分享

技术分享
 1 <head>
 2     <title>{title}</title>
 3 </head>
 4 <body>
 5     <table align="center" width="95" border="1">
 6         <tr>
 7             <td width="10%"><b>标签:</b></td>
 8             <td>{title}</td>
 9         </tr>
10         <tr>
11             <td width="10%"><b>作者:</b></td>
12             <td>{author}</td>
13         </tr>
14         <tr>
15             <td width="10%"><b>时间:</b></td>
16             <td>{createTime}</td>
17         </tr>
18         <tr>
19             <td width="10%"><b>内容:</b></td>
20             <td>{content}</td>
21         </tr>
22     </table>
23 </body>
news.template

 

接下来使用IO流的InputStream将该文件读取到内存中

技术分享
 1 //读取HTML模板文件new.template
 2     public String readFile(String path) throws IOException{
 3 InputStream is=null;
 4         String result="";
 5         try {
 6             @SuppressWarnings("unused")
 7             int data=0;
 8             byte[] by =new byte[1024];
 9             is = new FileInputStream(path);
10             while((data=is.read(by))!=-1){
11                 //result+=(char)data;
12                 //result=new String(data);
13                 result=new String(by,0,by.length);
14             }
15         } catch (FileNotFoundException e) {
16             System.out.println("未找到new.template文件!");
17             e.printStackTrace();
18         }
19         finally{
20             System.out.println("创建成功!");
21             is.close();
22         }
23         //return result.toString();
24         return result;
25     }
String readFile(String path) throws IOException

 

编写方法toHTml()   替换模板文件,为每条新闻创建一个HTML文件来显示其信息

技术分享
 1 //读取数据库表,获取新闻列表信息(在此不做讲解)
 2 List<News> list = dao.allInfo();
 3 //编写方法  将从数据库中读取到的数据替换掉news.template文件中的占位符"{}"
 4 String template= fileio.readFile("D:\\news.template");
 5         
 6         //替换模板文件,为每条新闻创建一个HTML文件来显示其信息
 7         for (int i = 0; i < list.size(); i++) {
 8             //获取一条新闻信息
 9             News news=list.get(i);
10             //使用该条新闻信息替换对应占位符
11             String replacetr = new String();
12             replacetr=template;
13             //replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的
14             replacetr=replacetr.replace("{title}",news.getTitle());
15             replacetr=replacetr.replace("{author}",news.getAuthor());
16             replacetr=replacetr.replace("{createTime}",news.getDatetime().toString());
17             replacetr=replacetr.replace("{content}",news.getContent());
18             //为该条新闻生成HTML文件
19             String filepath="D:\\dbtohtml\\new"+i+".html";
20             
21             fileio.writeFile(filepath,replacetr);
toHtml() throws SQLException, IOException

 

最终结果如下

技术分享

 

java中使用String的replace方法替换html模板保存文件

标签:

原文地址:http://www.cnblogs.com/john69-/p/5468412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!