public static String getTopNewsTemp(List<Article> lists) {
//读取定义前台值变量的内容
String tempContent = TempUtils.getTempFile("topNew.tmp");
//定义前台样式并用{}定义值存放位置
// String format = "<div style=\"line-height:20px;\"><div style=\"width:350px;padding-right:5px;padding-left:5px;float:left;font-size:12px;\"><a href=\"{0}\" target=\"_blank\" title=\"{1}\">·{2}</a></div><div style=\"font-size:12px;float:right;text-aligh:left;padding-right:15px;\">{3}</div></div>";
String format = "<li><a href=\"{0}\">{2}</a><em>{3}</em></li>";
StringBuffer allContent = new StringBuffer();
StringBuffer list = new StringBuffer();
//遍历添加数据到前台可读取样式
for (int index = 0; index < lists.size(); index++) {
Article article = lists.get(index);
String title = article.getTitle();
//添加样式 和 值
list.append(MessageFormat.format(format, article.getUrl(), title,
getShortTitle(title, 27),
getShortDate(article.getCreateDate())));
}
//组装消息 tmp文件 #list# 获取该值
allContent.append(tempContent.replace("#list#", list.toString()));
return allContent.toString();
}
public class TempUtils {
public static String getTempFile(String fileName)
{
String str = TempUtils.class.getResource("../entity/temp/local/" + fileName).getFile();
File file = new File(str);
StringBuffer content = new StringBuffer();
try{
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader in = new BufferedReader(isr);
String s;
//throws IOException读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。
//返回:包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 抛出: IOException - 如果发生 I/O 错误
while ((s = in.readLine()) != null)
{
content.append(s);
}
isr.close();
in.close();
}catch(Exception ex)
{
return "处理读取模板时,出现错误,请检查 " + str + " 路径";
}
return content.toString();
}
}