mysql 6

来源:互联网 发布:linux内核内存管理浅析 编辑:程序博客网 时间:2024/06/03 18:27
public class CMSManager {


private NewsDao dao = new NewsDaoImpl();
  //定义dao对象   得到 newimple  l类的方法 
public void replaceContent(){

String path = "d:/html/";

//    D盘下 html  文件 

String templateContent = FileUtil.readFile(path + "template.html", "GBK");

    利用 readline  读取文件内容 

System.out.println(templateContent);

List<News> list = dao.getNews();

                //getNews方法得到日期得到得到文章标题内容和作者

//把数据库的新闻内容通过循环写入到html文件中
for(News n : list){
String content = templateContent.replace("#{title}", n.getTitle()).
replace("#{author}", n.getAuthor()).
replace("#{date}", n.getPublishDate()).
replace("#{content}", n.getContent());

// System.out.println(content);
FileUtil.writeFile(content, path + n.getTitle() + ".html");
}

}    

   public class FileUtil {


public static String readFile(String fileName){
FileReader fr = null;
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try {
fr = new FileReader(fileName);

              // 读取名字为Filename  的文件 

br = new BufferedReader(fr);
String tmp = br.readLine();

          //zhenhan
while(tmp != null){
sb.append(tmp).append("\r\n");

//利用StringBuffer 类中的 Append 方法 链接tmp
tmp = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(br != null){
br.close();
}
if(fr != null){
fr.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}

return sb.toString();
}


public static String readFile(String fileName, String charSet){
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try {
fis = new FileInputStream(fileName);
isr = new InputStreamReader(fis,charSet);
br = new BufferedReader(isr);
String tmp = br.readLine();
while(tmp != null){
sb.append(tmp).append("\r\n");
tmp = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(br != null){
br.close();
}
if(isr != null){
isr.close();
}
if(fis != null){
fis.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}

return sb.toString();
}

需要写入文件的内容
public static void writeFile(String content, String fileName){
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bw != null){
bw.close();
}
if(fw != null){
fw.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}

}


public static void main(String[] args) {
String c = FileUtil.readFile("d:/html/template.html");

//度文件 
FileUtil.writeFile(c, "d:/html/t1.html");

//写入文件
System.out.println("tesk ok");
}
}

0 0