简单的 html文件 转换为 jsp 文件 不损坏原文件实现

来源:互联网 发布:什么是即时通信软件 编辑:程序博客网 时间:2024/05/22 00:40
package alone.utils;import alone.utils.reflect.PropertieUtils;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Reader;import java.io.Writer;/** * 修改当前项目下所有的html 文件 为 jsp 页面 *  * @author alone_xuxu * @date 2017年7月19日 上午9:34:07 */public class JspUtils {   /**    * 主函数,执行这个之后当前项目下所有的 Html 文件都会生成与之对应的 Jsp 文件的    * @author alone_xuxu    * @date 2017年7月19日 上午9:34:38    */   public static void main(String[] args) {      //删除旧的html文件      //allHtmlReplaceTOJsp(true);            //不删除旧的html文件      allHtmlReplaceTOJsp(false);   }               /**    * 替换当前项目下所有的html 文件 为 jsp 文件,并且选择是否删除原始的html文件    * @author alone_xuxu    */   public static void allHtmlReplaceTOJsp(boolean deleteOldHtml){            //./表示当前项目路径下  //修改我们 html_to_jsp.properties 文件中的路径,就是修改在哪一个路径下的html改成jsp      String path = PropertieUtils.getProperty("html_to_jsp.properties","path");      File root = new File(path);      //查找和替换      findAllHtmlFileBuilderJspFile(root,deleteOldHtml);      }         /**    * 通过html文件生成jsp文件    * @param file    */   public static void createJspFileByHtmlFile(File file){            if(file == null) return;            //修改之前先要给这个文件首部添加jsp 头      String head = "<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%>";                  //生成新的文件      String path = file.getAbsolutePath();      String fileName = path.substring(0,path.lastIndexOf(".")) +".jsp";      System.out.println("即将生成的新的文件名:" + fileName );            //新的文件      File fileCopy = new File(fileName);                  BufferedReader br = null;      BufferedWriter bw = null;      try {         br = new BufferedReader(new FileReader(file));         bw = new BufferedWriter(new FileWriter(fileCopy));                  //写入jsp头内容         bw.write(head);         bw.newLine();                  /*          * 这里直接赋值html文件内容到新的文件中          */         String str = null;         while (true) {            if ((str = br.readLine()) != null) {               bw.write(str);               bw.newLine();            } else {               break;            }         }      } catch (Exception e) {         e.printStackTrace();      } finally {         closeReaderAndWriter(br, bw);      }   }         /**    * 查找和替换    * @author alone_xuxu    * @param root    */   private static void findAllHtmlFileBuilderJspFile(File root,boolean delete) {      //获取当当前目录下的所有文件信息      File[] files = root.listFiles();                  //如果没找着就不管了      if (files == null)  return;                  for (File file : files) {         if (file.isDirectory()) {                        // 如果是目录接着来啊            findAllHtmlFileBuilderJspFile(file,delete);                     } else if (file.isFile() && file.getName().endsWith(".html")) {               //开始修改               createJspFileByHtmlFile(file);                              //如果调用者声明了,需要删除旧的html               if(delete){                  System.err.println("删除原始的html文件: "+file.getName());                  file.delete();               }         }      }   }         /**    * 关闭reder 和 writer 流    * @param reder    * @param writer    * @author alone_xuxu 2017年7月19日 上午10:15:07    */   public static void closeReaderAndWriter(Reader reder,Writer writer){      if(reder !=null)         try {            reder.close();         } catch (IOException e) {            e.printStackTrace();         }      if(writer !=null)         try {            writer.close();         } catch (IOException e) {            e.printStackTrace();         }   }}

阅读全文
0 0
原创粉丝点击