简单的 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
- 简单的 html文件 转换为 jsp 文件 不损坏原文件实现
- eclipse中html转换为jsp文件
- 【Bash】实现指定目录下的文件编码转换,以原文件名保存
- html input控件type为file上传文件、图片,及保存为原文件
- Html文件转CHM文件类--------用C#实现将html文件转换为chm文件
- Html文件转CHM文件类--------用C#实现将html文件转换为chm文件
- html文件转换成jsp文件的问题
- 静态文件(HTML)与动态文件(JSP)的转换
- 将html文件转换成jsp文件
- 将html文件转换为pdf格式的文件
- 将CHM文件转换为HTML文件
- 将Java文件转换为HTML文件
- JSP实现简单的文件上传
- JSP实现简单的文件下载功能
- 使用bootstrap的html文件转换成jsp…
- IntelliJ IDEA的Rebuild顺序,运行的文件跟原文件不相同
- JSP防止不继续生成html文件
- 写入出字符串到一个文件而不覆盖原文件的内容(追加)
- 《android开发艺术探索笔记》Part7、Android动画深入分析
- 解决:CBitmapButtion在debug情况下,出现debug assertion failed的问题
- 直播发展如此迅猛,在未来会有哪些新的趋势?
- Slam研究点介绍
- 1066. 图像过滤(15)
- 简单的 html文件 转换为 jsp 文件 不损坏原文件实现
- 数据库创建索引的注意事项
- jq操作select下拉框
- postman使用详解
- 【Linux】linux/ubuntu系统下怎么保存命令行终端的输入、输出到指定日志文件
- go对mysql的简单操作
- STL中list的使用及模拟实现
- webservice wsdl引用
- 【每天积累一点点】Humans of New York